Merge branch 'master' of ssh://git.cweiske.de/~/git/usb-wde1-tools
[usb-wde1-tools.git] / init.d / usb-wde-log
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          usb-wde-log
4 # Required-Start:    $remote_fs $syslog
5 # Required-Stop:     $remote_fs $syslog
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: Logs temperature data
9 ### END INIT INFO
10
11 # Author: Christian Weiske <cweiske@cweiske.de>
12
13 # PATH should only include /usr/* if it runs after the mountnfs.sh script
14 PATH=/sbin:/usr/sbin:/bin:/usr/bin
15 DESC="Logs temperature data"
16 NAME=usb-wde-log
17 DAEMON=/bin/sh
18 DAEMON_ARGS="/usr/local/src/usb-wde1-tools/munin/usb-wde1-log-last.sh"
19 PIDFILE=/var/run/$NAME.pid
20 SCRIPTNAME=/etc/init.d/$NAME
21
22 # Exit if the package is not installed
23 [ -x "$DAEMON" ] || exit 0
24
25 . /lib/init/vars.sh
26 . /lib/lsb/init-functions
27
28 do_start()
29 {
30         # Return
31         #   0 if daemon has been started
32         #   1 if daemon was already running
33         #   2 if daemon could not be started
34         start-stop-daemon --start --background --make-pidfile --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
35                 || return 1
36         start-stop-daemon --start --background --make-pidfile --quiet --pidfile $PIDFILE --exec $DAEMON -- \
37                 $DAEMON_ARGS \
38                 || return 2
39 }
40
41 do_stop()
42 {
43         # Return
44         #   0 if daemon has been stopped
45         #   1 if daemon was already stopped
46         #   2 if daemon could not be stopped
47         #   other if a failure occurred
48         [ ! -f "$PIDFILE" ] && echo "$PIDFILE not found" && return 1
49         #kill all children (socat, line logger)
50         ppid=`cat "$PIDFILE"`
51         for i in `ps -ef| awk '$3 == '${ppid}' { print $2 }'`
52         do
53                 kill $i
54         done
55         start-stop-daemon --stop --quiet --retry=TERM/10/KILL/5 --pidfile $PIDFILE
56         RETVAL="$?"
57         [ "$RETVAL" = 2 ] && return 2
58         [ "$RETVAL" = 0 ] && rm -f $PIDFILE
59         return "$RETVAL"
60 }
61
62 do_reload() {
63         start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
64         return 0
65 }
66
67 case "$1" in
68   start)
69         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
70         do_start
71         case "$?" in
72                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
73                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
74         esac
75         ;;
76   stop)
77         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
78         do_stop
79         case "$?" in
80                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
81                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
82         esac
83         ;;
84   status)
85         pid=`pgrep -f "$DAEMON $DAEMON_ARGS"`
86         [ "$pid" -gt 0 ] && echo "$NAME is running" && exit 0
87         echo "$NAME is NOT running"
88        ;;
89   restart|force-reload)
90         log_daemon_msg "Restarting $DESC" "$NAME"
91         do_stop
92         case "$?" in
93           0|1)
94                 do_start
95                 case "$?" in
96                         0) log_end_msg 0 ;;
97                         1) log_end_msg 1 ;; # Old process is still running
98                         *) log_end_msg 1 ;; # Failed to start
99                 esac
100                 ;;
101           *)
102                 # Failed to stop
103                 log_end_msg 1
104                 ;;
105         esac
106         ;;
107   *)
108         #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
109         echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
110         exit 3
111         ;;
112 esac
113
114 :