-
Notifications
You must be signed in to change notification settings - Fork 10
/
daemon.sh
executable file
·125 lines (112 loc) · 2.47 KB
/
daemon.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
### BEGIN INIT INFO
# Provides: swipl-website
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/stop SWI-Prolog web server
### END INIT INFO
# Installation
#
# 1. Copy this file to /etc/init.d/<server>
# 2. Edit the Configuration section below
# 3. Run
# % update-rc.d <server> defaults
# % /etc/init.d/<server> start
# Configuration section
#
SWIPL=/home/janw/bin/swipl
DIR=/home/janw/src/plweb
SCRIPT=daemon.pl
USER=www
PORT=80
DAEMONARGS=
# Uncomment to only listen for connections from localhost
# DAEMONARGS="$DAEMONARGS --ip=localhost"
HTTPID=swipl-httpd-$PORT
PIDFILE=/var/run/$HTTPID.pid
SYSLOG=$HTTPID
# End of normal configuration
. /lib/lsb/init-functions
test -f /etc/default/rcS && . /etc/default/rcS
DAEMONARGS="$DAEMONARGS --port=$PORT --user=$USER --fork"
if [ ! -z "$SYSLOG" ]; then DAEMONARGS="$DAEMONARGS --syslog=$SYSLOG"; fi
if [ ! -z "$PIDFILE" ]; then DAEMONARGS="$DAEMONARGS --pidfile=$PIDFILE"; fi
pidofserver()
{ if [ -f "$PIDFILE" ]; then
cat "$PIDFILE"
else
ps aux | grep "[0-9] *$SWIPL.*--port=$PORT" 2>/dev/null | awk '{print $2}'
fi
}
running()
{ if [ -z "$1" ]; then return 1; fi
if kill -0 $1 2> /dev/null; then
return 0
else
return 1
fi
}
waitserver()
{ i=0;
while running $1; do
if [ $i = '60' ]; then
return 1;
else
if [ $i = '0' ]; then
echo -n " ... waiting "
else
echo -n "."
fi
i=$(($i+1))
sleep 1
fi
done
}
case $1 in
start)
log_daemon_msg "Starting web server" "$HTTPID"
if (cd $DIR && $SWIPL -q -f $SCRIPT -- $DAEMONARGS); then
log_end_msg 0
else
log_end_msg 1
fi
;;
stop)
log_daemon_msg "Stopping web server" "$HTTPID"
PID=$(pidofserver)
kill $PID
if waitserver $PID; then
log_end_msg 0
else
kill -9 $PID
waitserver $PID
fi
;;
reload)
PID=$(pidofserver)
kill -HUP $PID
;;
restart)
$0 stop && $0 start
;;
status)
PID=$(pidofserver)
if running "$PID"; then
echo "SWI-Prolog HTTP server is running (pid $PID)."
exit 0
else
echo "SWI-Prolog HTTP server is NOT running."
if [ -e $PIDFILE ]; then
exit 1
else
exit 3
fi
fi
;;
*)
log_success_msg "Usage: /etc/init.d/swipl-httpd {start|stop|restart|status}"
exit 1
esac