Thursday, December 1, 2011

Adding a new service to Fedora 16

Suppose that you have a new service that you want to control with the 'service' command, or that you want to start every time the system boot.
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/bash
#
# 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
The 'chkconfig' and 'description' lines are mandatory for the script to be used with the chkconfig tool.

From chkconfig's man page:
For example, random.init has these three two  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