How can I make a script in /etc/init.d start at boot?
If you are on a Red Hat based system, as you mentioned, you can do the following:
- Create a script and place in
/etc/init.d
(e.g/etc/init.d/myscript
). The script should have the following format:
#!/bin/bash
# chkconfig: 2345 20 80
# description: Description comes here....
# Source function library.
. /etc/init.d/functions
start() {
# code to start app comes here
# example: daemon program_name &
}
stop() {
# code to stop app comes here
# example: killproc program_name
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
# code to check status of app comes here
# example: status program_name
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
The format is pretty standard and you can view existing scripts in /etc/init.d
. You can then use the script like so /etc/init.d/myscript start
or chkconfig myscript start
. The ckconfig
man page explains the header of the script:
> This says that the 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.
The example start, stop and status code uses helper functions defined in /etc/init.d/functions
Enable the script
$ chkconfig --add myscript $ chkconfig --level 2345 myscript on
Check the script is indeed enabled - you should see "on" for the levels you selected.
$ chkconfig --list | grep myscript
You test, what runlevel your machine normally starts into.
runlevel
Often this is 5 or 2 - there are various conventions, but nothing really established, afaik. Ubuntu uses 2, while former distribution I used always used
- 1 Single user (super user)
- 2 multi user
- 3 multi user + network
- 4 not used / user definable
- 5 multi user, network + X11
Then you make a symlink from your init-script, maybe /etc/init.d/foobar
to /etc/rc2.d/SXYfoobar
S means 'Start this script in this runlevel (here: 2). XY is a two-digit decimal number, which is relevant for the sequence, the scripts are started.
If you depend on script S45barfoo to be run before you, and S55foofoo is depending on your script, you would choose xy between 45 and 55. For equal numbers the boot order is undefined.
Ubuntu meanwhile switched (is switching) to another startup procedure, called upstart
.
And note: Not always the links link to /etc/rcX.d
- sometimes it is /etc/init/rcX.d
or something similar, but it should be easy to find, somewhere below /etc.
If you want to start something at the end of the starting scripts, /etc/rc.local
would be file to look for, but if it depends on X11 already running, you might look for an autostart-option of your desktop environment, or /etc/X11/Xsession.d/
with a similar pattern as described above.
If you depend on the network being up, there is a separate directory (if-up.d), and for mounted devices like external USB-drives /etc/udev/rules.d/
.
As Naftuli Tzvi Kay asked about Debian above: Beginning with Debian 6, your script should contain a LSB (Linux Standards Base) header which indicates its dependencies and capabilities (see debian wiki page).
If a LSB header is present, you can use insserv
to include your script in the boot process (see another debian wiki page).