-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartupscript.sh
110 lines (97 loc) · 2.36 KB
/
startupscript.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
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
#!/bin/bash
# /etc/init.d/nodered
# version 0.3.9 2014-10-25 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: EEM
# Required-Start: $local_fs $remote_fs screen-cleanup
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start nodered
# Description: Starts the NodeRED server
### END INIT INFO
#Settings
SERVICE='AppServer.js'
OPTIONS=''
USERNAME='iot'
APP_PATH="/home/$USERNAME/repos/AppServer"
HISTORY=1024
NAME='appserver'
NODE_VERSION='0.12.7'
NODE=/home/$USERNAME/.nvm/versions/node/v$NODE_VERSION/bin/node
INVOCATION="$NODE $SERVICE"
ME=`whoami`
as_user() {
if [ "$ME" = "$USERNAME" ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
my_start() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
cd $APP_PATH
as_user "cd $APP_PATH && screen -h $HISTORY -dmS $NAME $INVOCATION"
sleep 7
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is now running."
else
echo "Error! Could not start $SERVICE!"
echo "CMD: cd $APP_PATH && screen -h $HISTORY -dmS $NAME $INVOCATION"
fi
fi
}
my_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
#as_user "screen -p 0 -S nodered -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
#as_user "screen -p 0 -S nodered -X eval 'stuff \"save-all\"\015'"
##sleep 10
##as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
##sleep 7
pkill -u iot -f $SERVICE
sleep 7
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Error! $SERVICE could not be stopped."
else
echo "$SERVICE is stopped."
fi
}
#Start-Stop here
case "$1" in
start)
my_start
;;
stop)
my_stop
;;
restart)
my_stop
my_start
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|update|backup|status|restart|command \"server command\"}"
exit 1
;;
esac
exit 0