-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.sh
More file actions
225 lines (192 loc) · 4.41 KB
/
manage.sh
File metadata and controls
225 lines (192 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# Application configuration
APP_FILE="main.py"
PID_FILE=".app.pid"
LOG_DIR="logs"
LOG_FILE="$LOG_DIR/app.log"
ERROR_LOG="$LOG_DIR/error.log"
VENV_DIR=".venv"
# Create log directory
mkdir -p $LOG_DIR
# Print colored output
print_success() {
echo "✓ $1"
}
print_error() {
echo "✗ $1"
}
print_info() {
echo "ℹ $1"
}
# Start the application
start() {
# Check if already running
if [ -f "$PID_FILE" ]; then
PID=$(cat $PID_FILE)
if ps -p $PID > /dev/null 2>&1; then
print_error "App is already running (PID: $PID)"
exit 1
fi
fi
# Create virtual environment if it doesn't exist
if [ ! -d "$VENV_DIR" ]; then
print_info "Creating virtual environment..."
python3 -m venv $VENV_DIR
fi
# Activate virtual environment
source $VENV_DIR/bin/activate
# Install dependencies if requirements.txt exists
if [ -f "requirements.txt" ]; then
print_info "Checking dependencies..."
pip install -q -r requirements.txt
fi
# Read port from .env (default to 5000)
PORT=5000
if [ -f ".env" ]; then
PORT_VAL=$(grep "^PORT=" .env | cut -d '=' -f2)
if [ ! -z "$PORT_VAL" ]; then
PORT=$PORT_VAL
fi
fi
print_info "Starting Code Agent API Wrapper..."
# Start app in background with nohup
nohup python $APP_FILE > $LOG_FILE 2> $ERROR_LOG &
# Save PID to file
echo $! > $PID_FILE
print_success "Server started! (PID: $!)"
print_info "URL: http://localhost:$PORT"
print_info "Log: tail -f $LOG_FILE"
}
# Stop the application
stop() {
if [ ! -f "$PID_FILE" ]; then
print_error "Server is not running."
exit 1
fi
PID=$(cat $PID_FILE)
if ps -p $PID > /dev/null 2>&1; then
print_info "Stopping server (PID: $PID)..."
kill $PID
sleep 1
# Force kill if still running
if ps -p $PID > /dev/null 2>&1; then
print_info "Force killing server..."
kill -9 $PID
fi
rm $PID_FILE
print_success "Server stopped."
else
print_error "Process not found. Cleaning up PID file."
rm $PID_FILE
exit 1
fi
}
# Check server status
status() {
if [ ! -f "$PID_FILE" ]; then
print_error "Server is NOT running."
exit 1
fi
PID=$(cat $PID_FILE)
if ps -p $PID > /dev/null 2>&1; then
print_success "Server is running (PID: $PID)"
# Read port from .env
PORT=5000
if [ -f ".env" ]; then
PORT_VAL=$(grep "^PORT=" .env | cut -d '=' -f2)
if [ ! -z "$PORT_VAL" ]; then
PORT=$PORT_VAL
fi
fi
print_info "URL: http://localhost:$PORT"
else
print_error "Process not found (stale PID file). Cleaning up."
rm $PID_FILE
exit 1
fi
}
# View logs
logs() {
if [ ! -f "$LOG_FILE" ]; then
print_error "Log file not found: $LOG_FILE"
exit 1
fi
tail -f $LOG_FILE
}
# View error logs
errors() {
if [ ! -f "$ERROR_LOG" ]; then
print_error "Error log file not found: $ERROR_LOG"
exit 1
fi
tail -f $ERROR_LOG
}
# Restart the application
restart() {
if [ -f "$PID_FILE" ]; then
stop
sleep 2
fi
start
}
# Clean up old log files
clean() {
print_info "Cleaning up log files..."
rm -f $LOG_FILE $ERROR_LOG
print_success "Log files cleaned."
}
# Print usage information
usage() {
cat << EOF
Code Agent API Wrapper Management Script
Usage: ./manage.sh [COMMAND]
Commands:
start Start the application
stop Stop the application
status Check application status
restart Restart the application
logs View application logs (tail -f)
errors View error logs (tail -f)
clean Clean up log files
help Show this help message
Examples:
./manage.sh start
./manage.sh stop
./manage.sh status
./manage.sh restart
./manage.sh logs
EOF
}
# Main script logic
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
logs)
logs
;;
errors)
errors
;;
clean)
clean
;;
help|--help|-h)
usage
;;
*)
print_error "Unknown command: $1"
echo ""
usage
exit 1
;;
esac