-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·127 lines (118 loc) · 2.41 KB
/
Copy pathrun
File metadata and controls
executable file
·127 lines (118 loc) · 2.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
#!/bin/bash
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
# error
#
# show an error message and exit
#
# params:
# 1: l_msg - the message to display
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error: $l_msg" 1>&2
exit 1
}
#
# show the usage
#
usage() {
echo "usage: $0 [-s] [-h]"
echo " -h: show this usage"
echo " -s: start as server only"
}
export PYTHONPATH=""
#
# open the given url waiting for the given number of seconds
#
# param #1: the url to open
# param #2: the number of loops to wait
# param #3: the sleep time per loop
openUrl() {
local l_url="$1"
local l_loops="$2"
local l_sleep="$3"
local l_count=1
local l_done=0
until [ $l_done -eq 1 ]
do
l_count=$((l_count+1))
if [ "$l_count" -ge "$l_loops" ]
then
echo "giving up to wait for $l_url"
l_done=1
fi
status=$(curl -Is $l_url | head -1)
echo "waiting $l_count/$l_loops for $l_url: $status"
case $status in
*200*OK*) open $l_url
l_done="1" ;;
esac
sleep $l_sleep
done
}
#
# kill the given process by name if it is running
#
# param #1: l_name: the name to search for
killifrunning() {
local l_name="$1"
pgrep -fl "$l_name"
if [ $? -eq 0 ]
then
color_msg $blue "killing running $l_name server"
sudo pkill -f "$l_name"
fi
}
startServer() {
local l_logdir=/var/log/pi-q-robot
local l_logfile=pi-q-robot.log
color_msg $blue "starting server only"
if [ ! -d $l_logdir ]
then
sudo mkdir -p $l_logdir
sudo chmod 770 $l_logdir
fi
sudo chown $USER $l_logdir
sudo chgrp users $l_logdir
nohup python3 $pyapp > $l_logdir/$l_logfile 2>&1 &
color_msg $green "log is at $l_logdir/$l_logfile"
}
pyapp=pi-q-robot.py
killifrunning $pyapp
port=5002
# commandline option
while [ "$1" != "" ]
do
option=$1
shift
case $option in
-h|--help)
usage
exit 0;;
-s)
startServer
exit 0
;;
*)
;;
esac
done
color_msg $blue "starting in client mode"
openUrl "http://localhost:$port" 60 0.5&
sudo python3 $pyapp