-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
80 lines (73 loc) · 1.83 KB
/
start.sh
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
#!/bin/bash
PYTHON_SCRIPT="./YoloRecognition.py"
PID_FILE="/tmp/YoloRecognition.pid"
start_script() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if kill -0 $PID > /dev/null 2>&1; then
echo "Script is already running with PID $PID."
return
else
echo "Found PID file but no process is running. Cleaning up..."
rm -f "$PID_FILE"
fi
fi
echo "Starting script..."
nohup python3 "$PYTHON_SCRIPT" > /dev/null 2>&1 &
echo $! > "$PID_FILE"
sleep 1 # 确保进程已经启动
PID=$(cat "$PID_FILE")
if kill -0 $PID > /dev/null 2>&1; then
echo "Script started with PID $PID."
else
echo "Failed to start script."
rm -f "$PID_FILE"
fi
}
stop_script() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if kill -0 $PID > /dev/null 2>&1; then
echo "Stopping script with PID $PID..."
kill -9 $PID
rm -f "$PID_FILE"
echo "Script stopped."
else
echo "No process found with PID $PID. Cleaning up..."
rm -f "$PID_FILE"
fi
else
echo "No PID file found. Script is not running."
fi
}
status_script() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if kill -0 $PID > /dev/null 2>&1; then
echo "Script is running with PID $PID."
else
echo "Found PID file but no process is running with PID $PID."
fi
else
echo "Script is not running."
fi
}
case "$1" in
start)
start_script
;;
stop)
stop_script
;;
restart)
stop_script
start_script
;;
status)
status_script
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac