Working with mono-service on a raspberry pi

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

To stop a service

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:

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