- Install daemontools
$ sudo apt-get install daemontools daemontools-run
- Create the service directory if not created by daemontools
$ sudo mkdir -p /etc/service
- Create the directory for the service you want to be monitored
$ sudo mkdir /etc/service/testservice
- Create the run script that will be used by daemontools to start the service automatically. This can be created by using the following command (or similar), however the filename must be
run
:
$ sudo vi /etc/service/testservice/run
#!/bin/bash
exec <command to run>
Then give the run script execute access:
$ sudo chmod u+x /etc/service/testservice/run
The daemontools will start the run script which will then become the command specified.
You can use $ sudo svstat /etc/service/testservice
to check the status of the service. Additionally you can use the svc
command to start, stop, restart, and provide other control to services. Just use $ man svc
to see what it is capable of.
If you want to run the service as a user other than root (e.g. the pi user), look at the following example:
#!/bin/bash
exec sudo -u pi bash -e /home/pi/test.sh
This run script will cause daemontools to monitor the /home/pi/test.sh
script running as the pi user.