I was working with on a .net service that I wanted to run on my raspberry pi. Initially this was fairly easy but then I wanted to run it on startup as a service.
After a bit of digging I found out about mono-service and mono-service2 (for .net 1 and .net 2 respectively)
Working with this is as simply as …
To start a service
1 |
mono-service2 something.exe |
To stop a service
1 |
kill `cat /tmp/something.exe.lock` |
Woah – ok stopping isn’t so straightforward …
Basically when the service starts it puts a lock file in the /tmp folder. This is just a text file that contains the process id (pid) of the exe that is running. By doing the cat, and passing that to kill, the service will stop.
Next up was to get it to run on start up.
I found a useful bash script to start, stop and restart the service … http://www.geekytidbits.com/start-stop-daemon-with-mono-service2/
Unfortunately this didn’t work immediately for me as my version of linux was built differently and didn’t have some of the parts this script depended on.
My finished script now looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#!/bin/sh daemon_name=BPI PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/bin/mono/mono-service2 NAME=BPI DESC=BPI . /lib/lsb/init-functions MONOSERVER=$(which mono-service2) MONOSERVER_PID=$(cat /tmp/BPI.lock) case "$1" in start) log_daemon_msg "Starting BPI" if [ -z "${MONOSERVER_PID}" ]; then ${MONOSERVER} -l:/tmp/BPI.lock -d:/home/pi/bpi/current/ /home/pi/bpi/current/bpi.daemon.exe log_end_msg 0 else -l log_failure_msg "BPI is already running!" exit 1 fi ;; stop) log_daemon_msg "Stopping BPI" if [ -n "${MONOSERVER_PID}" ]; then kill ${MONOSERVER_PID} rm /tmp/BPI.lock log_end_msg 0 else log_failure_msg "BPI is not running" exit 1 fi ;; restart) $0 stop sleep 1 $0 start log_end_msg 0 ;; *) log_action_msg "usage: $0 {start|stop|restart}" esac exit 0 log_end_msg 0 |
This was put in \etc\init.d\bpi.sh and I did a chmod to make it match the others in that directory.
Note that there are a couple more parameters to our start command … -l to tell it where to put the lock file and -d to give it a start directory (my application writes logs and they would appear in funny places without this).
Finally the following command sets things up to start and stop in different runlevels:
sudo update-rc.d bpi.sh defaults