You will need to put a wrapper script for your service under /etc/rc.d that must be owned by root. By convention it does not need an extension. In this example my wrapper script is called "myscript". The header is required to have a certain format (see below for an example) and the operating system will check for certain tokens in the comments. You can create different functions in the script corresponding to different conditions such as start, stop, status, restart, or reload. To keep it simple I will just deal with start and stop here. The following script is an example of a wrapper script for the script that you want to run. The actual script that you run will be called from this script. In this example I have called the script to be run "myservice.sh".
[root@myserver] /etc/rc.d/init.d>: more myscript #!/bin/sh # # myscript # # chkconfig: 2345 25 90 # description: Start/stop some services # # Source function library. . /etc/rc.d/init.d/functions RETVAL=0 prog="myservice" start() { echo -n $"Starting $prog:" RETVAL=$? [ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog echo /usr/bin/myservice.sh START } stop() { echo -n $"Stopping $prog:" killproc $prog -TERM RETVAL=$? [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog echo /usr/bin/myservice.sh STOP } case "$1" in start) start ;; stop) stop ;; *) echo $"Usage: $0 {start|stop}" RETVAL=1 esac exit $RETVAL #
Now you will set up your wrapper script to run on reboot. Linux uses a system of directories under /etc/rc.d to determine what gets run at boot time:
[root@myserver] /etc/rc.d>: ls -hal total 136K drwxr-xr-x 10 root root 4.0K May 31 2011 . drwxr-xr-x 103 root root 12K Apr 19 08:44 .. drwxr-xr-x 2 root root 4.0K Apr 19 15:40 init.d -rwxr-xr-x 1 root root 2.3K Jul 4 2009 rc drwxr-x--- 2 root root 4.0K Apr 3 10:36 rc0.d drwxr-x--- 2 root root 4.0K Apr 3 10:36 rc1.d drwxr-x--- 2 root root 4.0K Apr 19 04:34 rc2.d drwxr-x--- 2 root root 4.0K Apr 19 13:31 rc3.d drwxr-x--- 2 root root 4.0K Apr 19 04:34 rc4.d drwxr-x--- 2 root root 4.0K Apr 19 04:34 rc5.d drwxr-x--- 2 root root 4.0K Apr 3 10:36 rc6.d -rwxr-xr-x 1 root root 220 May 31 2011 rc.local -rwxr-xr-x 1 root root 27K Jul 4 2009 rc.sysinit
Each directory corresponds to a different Linux Run Level. In this example I will assume that we will set the script to run when the server boots into run levels 3 or 5.
Run level definitions from the Red Hat Linux documentation:
0 — Halt
1 — Single-user text mode
2 — Not used (user-definable)
3 — Full multi-user text mode
4 — Not used (user-definable)
5 — Full multi-user graphical mode (with an X-based login screen)
6 — Reboot
To set up the wrapper script to run you will create a link from the proper directories to the wrapper script in /etc/init.d like so:
[root@myserver /etc/rc.d/rc3.d]# ln -s ../init.d/myscript S99myscript [root@myserver /etc/rc.d/rc3.d]# cd ../rc5.d [root@myserver /etc/rc.d/rc5.d]# ln -s ../init.d/myscript S99myscript
The name of the link is significant. The first letter 'S' stands for service. The next two digits determine the order that this service will be started. In my example the script will be run as one of the last ones on start because it is set to 99. This is useful if you have dependencies on other services.
To test, reboot the server and check that your service is running.