master
sh 84 lines 1.98 KB
Raw
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides: <NAME>
4 # Required-Start: $local_fs $network $named $time $syslog
5 # Required-Stop: $local_fs $network $named $time $syslog
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Description: <DESCRIPTION>
9 ### END INIT INFO
10
11 SCRIPT=/usr/local/mesh/meshagent
12 RUNAS=root
13
14 PIDFILE=/var/run/meshagent.pid
15 LOGFILE=/var/log/meshagent.log
16
17 start() {
18 if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
19 echo 'Service already running' >&2
20 return 1
21 fi
22 echo 'Starting service…' >&2
23 local CMD="$SCRIPT -exec \"var child; process.on('SIGTERM', function () { child.removeAllListeners('exit'); child.kill(); process.exit(); }); function start() { child = require('child_process').execFile(process.execPath, [process.argv0, \"\"]); child.stdout.on('data', function (c) { }); child.stderr.on('data', function (c) { }); child.on('exit', function (status) { start(); }); } start();\" &> \"$LOGFILE\" & echo \$!"
24
25 cd /usr/local/mesh
26 su -c "$CMD" $RUNAS > "$PIDFILE"
27 echo 'Service started' >&2
28 }
29
30 stop() {
31 if [ ! -f "$PIDFILE" ]; then
32 echo 'Service not running' >&2
33 return 1
34 else
35 pid=$( cat "$PIDFILE" )
36 if kill -0 $pid 2>/dev/null; then
37 echo 'Stopping service…' >&2
38 kill -15 $pid
39 echo 'Service stopped' >&2
40 else
41 echo 'Service not running'
42 fi
43 rm -f $"PIDFILE"
44 fi
45 }
46 restart(){
47 stop
48 start
49 }
50 status(){
51 if [ -f "$PIDFILE" ]
52 then
53 pid=$( cat "$PIDFILE" )
54 if kill -0 $pid 2>/dev/null; then
55 echo "meshagent start/running, process $pid"
56 else
57 echo 'meshagent stop/waiting'
58 fi
59 else
60 echo 'meshagent stop/waiting'
61 fi
62
63 }
64
65
66 case "$1" in
67 start)
68 start
69 ;;
70 stop)
71 stop
72 ;;
73 restart)
74 stop
75 start
76 ;;
77 status)
78 status
79 ;;
80 *)
81 echo "Usage: service meshagent {start|stop|restart|status}"
82 ;;
83 esac
84 exit 0