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