root/projects/Twisted-Goodies/trunk/misc/etc_init.d_simpleserver

Revision 38, 2.9 kB (checked in by edsuom, 1 year ago)

Done writing untested master service code and organizing support files

Line 
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          simpleserver
4 # Required-Start:    $local_fs $remote_fs
5 # Required-Stop:     $local_fs $remote_fs
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      S 0 1 6
8 # Short-Description: Twisted Internet Servers
9 # Description:       Dirt-Simple Twisted-Based Internet Servers
10 ### END INIT INFO
11
12 # Author: Ed Suominen <ed@eepatents.com>
13 #
14 # Please remove the "Author" lines above and replace them
15 # with your own name if you copy and modify this script.
16
17 # Do NOT "set -e"
18
19 # PATH should only include /usr/* if it runs after the mountnfs.sh script
20 PATH=/usr/sbin:/usr/bin:/sbin:/bin
21 DESC="Simpleserver"
22 NAME=simpleserver
23 DAEMON=/usr/bin/twistd
24 TACFILE=/etc/$NAME/server.tac
25 LOGFILE=/var/log/$NAME.log
26 PIDFILE=/var/run/$NAME.pid
27 SCRIPTNAME=/etc/init.d/$NAME
28
29 # Exit if the package is not installed
30 [ -x "$DAEMON" ] || exit 0
31
32 # Read configuration variable file if it is present
33 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
34
35 # Load the VERBOSE setting and other rcS variables
36 [ -f /etc/default/rcS ] && . /etc/default/rcS
37
38
39 log_daemon_msg () {
40     echo -e "${1}..."
41 }
42
43 log_end_msg () {
44     if [ $1 -eq 0 ]
45         then
46         echo "[OK]"
47     else
48         echo "[Fail]"
49     fi
50 }
51
52 isrunning () {
53   if [ ! -f $PIDFILE ]
54       then
55       false
56   else
57       DAEMONPID=$(cat $PIDFILE)
58       if expr "$(cat /proc/$DAEMONPID/cmdline 2> /dev/null)" : "$DAEMON" > /dev/null 2>&1
59           then
60           true
61       else
62           false
63       fi
64   fi
65 }
66
67
68 #
69 # Function that starts the daemon/service
70 #
71 do_start()
72 {
73         # Return
74         #   0 if daemon has been started
75         #   1 if daemon was already running
76         #   2 if daemon could not be started
77     isrunning && return 1
78     $DAEMON --python=$TACFILE --logfile=$LOGFILE --pidfile=$PIDFILE || return 2
79 }
80
81 #
82 # Function that stops the daemon/service
83 #
84 do_stop()
85 {
86         # Return
87         #   0 if daemon has been stopped
88         #   1 if daemon was already stopped
89         #   2 if daemon could not be stopped
90         #   other if a failure occurred
91     isrunning && return 1
92     cat $PIDFILE |xargs kill
93         RETVAL="$?"
94         [ "$RETVAL" = 0 ] || return 2
95         return "$RETVAL"
96 }
97
98 case "$1" in
99   start)
100         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
101         do_start
102         case "$?" in
103                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
104                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
105         esac
106         ;;
107   stop)
108         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
109         do_stop
110         case "$?" in
111                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
112                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
113         esac
114         ;;
115   restart|force-reload)
116         log_daemon_msg "Restarting $DESC" "$NAME"
117         do_stop
118         case "$?" in
119           0|1)
120                 do_start
121                 case "$?" in
122                         0) log_end_msg 0 ;;
123                         1) log_end_msg 1 ;; # Old process is still running
124                         *) log_end_msg 1 ;; # Failed to start
125                 esac
126                 ;;
127           *)
128                 # Failed to stop
129                 log_end_msg 1
130                 ;;
131         esac
132         ;;
133   *)
134         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
135         exit 3
136         ;;
137 esac
138
139 :
Note: See TracBrowser for help on using the browser.