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
|
#!/bin/sh
### BEGIN INIT INFO
# Provides: swi-httpd
# 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
#
# 0. On Redhat/CentOS: yum install redhat-lsb
# 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=/usr/bin/swipl
DIR=/srv/swipl-httpd
SCRIPT=daemon.pl
USER=www-data
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)
MESSAGE="Starting web server $HTTPID"
if (cd $DIR && $SWIPL -q -s $SCRIPT -- $DAEMONARGS); then
log_success_msg $MESSAGE
else
log_failure_msg $MESSAGE
fi
;;
stop)
MESSAGE="Stopping web server $HTTPID"
PID=$(pidofserver)
kill $PID
if waitserver $PID; then
log_success_msg $MESSAGE
else
kill -9 $PID
if waitserver $PID; then
log_success_msg $MESSAGE
else
log_failure_msg $MESSAGE
fi
fi
;;
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
|