Ubuntu: run service on system startup

Old fashioned way to create Linux daemon script and make it auto run.

Open editor
$ sudo nano myservice

and create the following script in home folder:

#! /bin/bash
#
start() {
echo “Starting <Service>”
}
stop() {
echo “Stopping <Service>”
}
restart() {
stop
start
}
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $“Usage: $0 {start|stop|restart}”
exit 1
esac
exit 0

Give it executable permissions:
$ sudo chmod +x myservice

Test the script:

$ ./myservice start
$ ./myservice stop
$ ./myservice restart

After the script is tested, copy it to /etc/init.d folder
$ sudo copy myservice /etc/init.d/myservice

And configure auto start with system
$ sudo update-rc.d myservice defaults
$ sudo update-rc.d myservice enable

When the service is not needed anymore, remove it
$ sudo update-rc.d -f myservice remove

Leave a Reply

Your email address will not be published.