Fix simbamond.init handling

See below for details of what is being fixed, and before+after test logs

1) First log without any changes, see some problems:
- start doesn't check if daemon already running (and so we get processes we can't control)
- [errors on shutdown - as simbamon kills itself but leaves the PID file, and simbamond.init errors if the process isn't there or if the PID file isn't there]
- if a process won't respond to SIGTERM (eg. due to being halted) then stopping it fails.

Anthony-Tooles-MacBook-Pro:Flickr Walk Through anthony$ ssh pi@192.168.1.12
pi@192.168.1.12's password:
Linux raspberrypi 3.12.20+ #687 PREEMPT Fri May 30 16:39:11 BST 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Jun 11 12:23:35 2014 from 192.168.1.5
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2077  0.4  0.6   2944   784 ?        S    12:25   0:00 /bin/bash /usr/sbin/simbamon
root      2112  0.0  0.4   1696   560 ?        Ss   12:25   0:00 startpar -f -- simbamond
pi        2239  0.0  0.6   3552   852 pts/0    S+   12:26   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond stop
[....] simbamon:: stopping...pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~ $ ps aux | grep simba
pi        2333  0.0  0.6   3548   800 pts/0    S+   12:26   0:00 grep --color=auto simba
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # stopping works.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2353  0.6  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2389  0.0  0.6   3548   800 pts/0    S+   12:27   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ # starting works
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond restart
[ ok ] Restarting SimBaMon: a simple battery monitor daemon: simbamon[....] simbamon:: stopping...[....] Started simbamon.
. ok
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ ps aux | grep simba
root      2467  0.5  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2554  0.0  0.6   3548   800 pts/0    S+   12:27   0:00 grep --color=auto simba
pi@raspberrypi ~ $ # restart did what it should
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2467  0.5  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
root      2597  0.5  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2641  0.0  0.6   3548   800 pts/0    S+   12:27   0:00 grep --color=auto simba
pi@raspberrypi ~ $ # not good: now got two processes, and one we won't be able to stop
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond stop
[....] simbamon:: stopping...pi@raspberrypi ~ $ close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2467  0.4  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2769  0.0  0.6   3548   800 pts/0    S+   12:28   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # so we've stopped the last one we started, but can't touch the first started one - yet status knows it is there
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo kill -9 2467
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
pi        2858  0.0  0.6   3548   800 pts/0    S+   12:28   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~ $ ps aux | grep simba
root      2878  0.3  0.6   4476   804 pts/0    S    12:28   0:00 /bin/bash /usr/sbin/simbamon
pi        2911  0.0  0.6   3548   800 pts/0    S+   12:28   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # back to normal
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ kill -s SIGSTOP 2878
-bash: kill: (2878) - Operation not permitted
pi@raspberrypi ~ $ sudo kill -s SIGSTOP 2878
pi@raspberrypi ~ $ ps aux | grep simba
root      2878  0.4  0.6   4476   804 pts/0    T    12:28   0:00 /bin/bash /usr/sbin/simbamon
pi        2975  0.0  0.6   3548   800 pts/0    S+   12:29   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond stop
[....] simbamon:: stopping...pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # so that appeared to work
pi@raspberrypi ~ $ ps aux | grep simba
root      2878  0.2  0.6   4476   804 pts/0    T    12:28   0:00 /bin/bash /usr/sbin/simbamon
pi        3022  0.0  0.6   3548   800 pts/0    S+   12:29   0:00 grep --color=auto simba
pi@raspberrypi ~ $ # but did nothing
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ kill -sSIGCONT 2878
-bash: kill: sSIGCONT: invalid signal specification
pi@raspberrypi ~ $ sudo kill -s SIGCONT 2878
pi@raspberrypi ~ $ # should have now handled the SIGTERM
pi@raspberrypi ~ $ ps aux | grep simba
pi        3055  0.0  0.6   3548   800 pts/0    S+   12:30   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~ $

2) Now with the fixes:
- cannot start more than one process
- a SIGHALT'd process will now be correctly killed
- see no errors on system shutdown

pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      2065  0.4  0.6   2944   784 ?        S    13:13   0:01 /bin/bash /usr/sbin/simbamon
root      2105  0.0  0.4   1696   560 ?        Ss   13:13   0:00 startpar -f -- simbamond
pi        2804  0.0  0.6   3552   852 pts/0    S+   13:19   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond stop
[ ok ] simbamon:: stopping...[....] simbamon stopped.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
pi        2922  0.0  0.6   3548   800 pts/0    S+   13:19   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # stopping works
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      2942  0.4  0.6   4476   804 pts/0    S    13:19   0:00 /bin/bash /usr/sbin/simbamon
pi        2998  0.0  0.6   3548   800 pts/0    S+   13:20   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # starting works
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond restart
[ ok ] Restarting SimBaMon: a simple battery monitor daemon: simbamon[....] simbamon:: stopping...[....] simbamon stopped.
[ ok ] Started simbamon.
. ok
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3112  0.4  0.6   4476   804 pts/0    S    13:20   0:00 /bin/bash /usr/sbin/simbamon
pi        3226  0.0  0.6   3548   800 pts/0    S+   13:20   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # restart works
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond start
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3112  0.4  0.6   4476   804 pts/0    S    13:20   0:00 /bin/bash /usr/sbin/simbamon
pi        3273  0.0  0.6   3548   800 pts/0    S+   13:21   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cat /var/run/simbamond.pid
3112
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # good - start no longer starts a second process, and existing process looks fine
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond stop
[ ok ] simbamon:: stopping...[....] simbamon stopped.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
pi        3427  0.0  0.6   3548   800 pts/0    S+   13:22   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # can still stop the process, and status is right
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3477  0.6  0.6   4476   804 pts/0    S    13:22   0:00 /bin/bash /usr/sbin/simbamon
pi        3513  0.0  0.6   3548   800 pts/0    S+   13:22   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # back to normal, now lets SIGHALT daemon
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ kill -s SIGSTOP 3477
-bash: kill: (3477) - Operation not permitted
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo kill -s SIGSTOP 3477
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3477  0.4  0.6   4476   804 pts/0    T    13:22   0:00 /bin/bash /usr/sbin/simbamon
pi        3591  0.0  0.6   3548   800 pts/0    S+   13:23   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond stop
[ ok ] simbamon:: stopping...[....] simbamon stopped.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # that took a while, presumably 30s + then SIGKILL did the job
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
pi        3658  0.0  0.6   3548   800 pts/0    S+   13:24   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~/mopi/pi-tronics/simbamon $