add init.d sctipt
[p2pool.git] / init.d / p2pool
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          p2pool
4 # Required-Start:    $network
5 # Required-Stop:     $network
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: p2pool startup script
9 # Description:       p2pool startup script
10 ### END INIT INFO
11
12 # Original author: Pavel A. Karoukin <pavel@yepcorp.com>
13
14 # Do NOT "set -e"
15
16 # PATH should only include /usr/* if it runs after the mountnfs.sh script
17 PATH=/sbin:/usr/sbin:/bin:/usr/bin
18 DESC="P2Pool"
19 NAME=p2pool
20 DAEMON=/opt/p2pool/run_p2pool.py
21 PIDFILE=/var/run/$NAME.pid
22 SCRIPTNAME=/etc/init.d/$NAME
23 CHUID=p2pool:p2pool
24 DAEMON_ARGS="--net novacoin --give-author 0"
25
26 # Exit if the package is not installed
27 [ -x "$DAEMON" ] || exit 0
28
29 # Read configuration variable file if it is present
30 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
31
32 # Load the VERBOSE setting and other rcS variables
33 . /lib/init/vars.sh
34
35 # Define LSB log_* functions.
36 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
37 . /lib/lsb/init-functions
38
39 #
40 # Function that starts the daemon/service
41 #
42 do_start()
43 {
44    # Return
45    #   0 if daemon has been started
46    #   1 if daemon was already running
47    #   2 if daemon could not be started
48    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
49       || return 1
50    start-stop-daemon --start --quiet --chuid $CHUID --pidfile $PIDFILE --exec $DAEMON -- \
51       $DAEMON_ARGS \
52       || return 2
53 }
54
55 #
56 # Function that stops the daemon/service
57 #
58 do_stop()
59 {
60    # Return
61    #   0 if daemon has been stopped
62    #   1 if daemon was already stopped
63    #   2 if daemon could not be stopped
64    #   other if a failure occurred
65    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
66    RETVAL="$?"
67    [ "$RETVAL" = 2 ] && return 2
68    # Wait for children to finish too if this is a daemon that forks
69    # and if the daemon is only ever run from this initscript.
70    # If the above conditions are not satisfied then add some other code
71    # that waits for the process to drop all resources that could be
72    # needed by services started subsequently.  A last resort is to
73    # sleep for some time.
74    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
75    [ "$?" = 2 ] && return 2
76    # Many daemons don't delete their pidfiles when they exit.
77    rm -f $PIDFILE
78    return "$RETVAL"
79 }
80
81 #
82 # Function that sends a SIGHUP to the daemon/service
83 #
84 do_reload() {
85    #
86    # If the daemon can reload its configuration without
87    # restarting (for example, when it is sent a SIGHUP),
88    # then implement that here.
89    #
90    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
91    return 0
92 }
93
94 case "$1" in
95   start)
96    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
97    do_start
98    case "$?" in
99       0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
100       2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
101    esac
102    ;;
103   stop)
104    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
105    do_stop
106    case "$?" in
107       0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
108       2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
109    esac
110    ;;
111   #reload|force-reload)
112    #
113    # If do_reload() is not implemented then leave this commented out
114    # and leave 'force-reload' as an alias for 'restart'.
115    #
116    #log_daemon_msg "Reloading $DESC" "$NAME"
117    #do_reload
118    #log_end_msg $?
119    #;;
120   restart|force-reload)
121    #
122    # If the "reload" option is implemented then remove the
123    # 'force-reload' alias
124    #
125    log_daemon_msg "Restarting $DESC" "$NAME"
126    do_stop
127    case "$?" in
128      0|1)
129       do_start
130       case "$?" in
131          0) log_end_msg 0 ;;
132          1) log_end_msg 1 ;; # Old process is still running
133          *) log_end_msg 1 ;; # Failed to start
134       esac
135       ;;
136      *)
137         # Failed to stop
138       log_end_msg 1
139       ;;
140    esac
141    ;;
142   *)
143    #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
144    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
145    exit 3
146    ;;
147 esac
148
149 :