-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovi_start.sh.sample
executable file
·104 lines (80 loc) · 2.49 KB
/
provi_start.sh.sample
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
#!/bin/bash
### User settings
DEV_DIR=""
### Obligatory settings
# directory of python environment for provi
APP_ENV_DIR=""
# directory where the provi application resides, i.e. the run.sh file
APP_DIR=""
############################################################################
### DON'T CHANGE ANYTHING BELOW UNLESS YOU REALLY KNOW WHAT YOU'RE DOING ###
############################################################################
SCRIPT_PATH=$0
SCRIPT_FILE=$(basename $0)
if [ "$1" == "help" ]; then
echo -e "
OPTIONS help status dev kill
help: prints this help
status: show if $SCRIPT_FILE is running or not
dev: for development, disables file logging and turns off
the automatic respawning
kill: kills the oldest previously started process started
by this script that is still running
no options: for production server, automatic respawning on crash
best run as 'nohup $SCRIPT_FILE > /dev/null 2>&1 &'
"
exit 0
fi
# log files
PASTER_LOG_FILE="paster.log"
RESPAWN_LOG_FILE="respawn.log"
# activate the python environment
cd $APP_ENV_DIR
source bin/activate
# os dependend ps command
if [ "$(uname)" == "Darwin" ]; then
PS_CMD="ps -ef"
else
PS_CMD="ps ef -o uid,pid,cmd"
fi
# find the first process (lowest PID) for SCRIPT_PATH
SCRIPT_PID=$($PS_CMD | grep $SCRIPT_PATH | grep -v grep | awk '{print $2}' | head -n 1)
# info
# echo -e "
# SCRIPT_FILE: $SCRIPT_FILE
# SCRIPT_PATH: $SCRIPT_PATH
# SCRIPT_PID: $SCRIPT_PID
# "
# show some status
if [ "$1" == "status" ]; then
if [ "$SCRIPT_PID" == "" -o "$SCRIPT_PID" == "$$" ]; then
echo "$SCRIPT_FILE not running"
else
echo "$SCRIPT_FILE running as process group $SCRIPT_PID"
fi
exit 0
fi
# if requested kill the application
if [ "$1" == "kill" ]; then
echo "killing process group $SCRIPT_PID"
# negate PID to kill the whole process group
kill -9 -$SCRIPT_PID
exit 0
fi
# start the application
cd $APP_DIR
if [ "$1" == "dev" ]; then
echo "dev mode, no logging, no automatic respawing"
./run.sh
else
# start the application and restart it if
# it does not ends with the exit code '0'
until ./run.sh --log-file $PASTER_LOG_FILE; do
EXIT_CODE=$?
PID=$$
echo "$(date): $SCRIPT_FILE (PID ${PID}) crashed with exit code ${EXIT_CODE}. Respawning..." >> $RESPAWN_LOG_FILE
# sleep for a second to remove strain from the system
# in case the application keeps crashing repeatedly
sleep 1
done
fi