This post explains how to do it in a few simple steps.
Step 1 - Create a service script
Create a file under /etc/init.d, let's call it 'myservice', and add the following to it:
#!/bin/bashThe 'chkconfig' and 'description' lines are mandatory for the script to be used with the chkconfig tool.
#
# chkconfig: 1000 50 50
#
# description: myservice service
#
start() {
/usr/share/myservice/start.sh
}
stop() {
/usr/share/myservice/stop.sh
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
From chkconfig's man page:
For example, random.init has thesethreetwo lines:
# chkconfig: 2345 20 80
# description: Saves and restores system...
This says that the random script should be started in levels 2, 3, 4, and 5, that its start priority should be 20, and that its stop priority should be 80.
Don't forget to make the script executable:
sudo chmod +x /etc/init.d/myservice
Step 2 - Using the script
sudo service myservice start
sudo service myservice stop
sudo service myservice restart
Step 3 - Run the script automatically at boot time (optional)
sudo systemctl enable myservice.service