Create service script on Ubuntu

Creating service script

$ vi myservice.sh

#!/bin/bash

...

case "$1" in

  (start)

    # start app command here

    ;;

  (stop)

    # stop app command here

    ;;

  (restart)

    # restart app command here

    ;;

  (*)

    echo "Unknown action!"

    exit 1

    ;;

esac

exit 0

Creating server configuration file

$ sudo vi /etc/systemd/myservice.service

[Unit]

Description=Job that runs the my application

[Service]

User=testuser

Group=testgroup

Type=forking

ExecStart=<path>/myservice.sh start

ExecStop=<path>myservice.sh stop

ExecReload=<path>myservice.sh restart

[Install]

WantedBy=multi-user.target

Testing your service

$ sudo systemctl start myservice

$ sudo systemctl status myservice

Enable your service

$ sudo systemctl enable myservice

Reference:

https://wiki.ubuntu.com/SystemdForUpstartUsers

http://stackoverflow.com/questions/33955604/start-a-python-script-at-boot-on-ubuntu

Written on December 2, 2016