Install Daemontools

sudo apt-get install daemontools daemontools-run

Create services

Services live in /etc/service.

  1. Create a new directory for a new service you want monitored:
    sudo mkdir /etc/service/myservice
    
  2. Make a new run script in the directory that will be automatically picked up and run by daemontools. It must be called run (with no file extension).
    sudo vim /etc/service/myservice/run
    

    Example run script:

    #!/bin/bash
    exec /usr/bin/python myscript.py
    
  3. Set permissions and watch it go!
    sudo chmod u+x /etc/service/myservice/run
    

Check on services

sudo svstat /etc/service/myservice

Start, stop, restart services

In general you want to use the svc command.

man svc
# svc seems to use relative paths so best used from the /etc/service directory.
cd /etc/service

# Kill a daemon and don't restart it.
sudo svc -k myservice

# Start a daemon.
sudo svc -u myservice

Running as $USER

If you want to run the service as a user other than root (e.g. your user), you want something like:

#!/bin/bash

exec sudo -u samliu /usr/bin/python /home/samliu/myscript.py