In managing Unix systems, you will often have occasion to have a process or action performed at boot time.

There are several ways of accomplishing this goal, but the right way is to create an init script and configure it to run at the proper run levels. Here’s how to do that under Ubuntu.

A valid LSB (Linux Standard Base) Init Script has 4 components:

  1. Provide, at least, the following actions: start, stop, restart, force-reload, and status (actually optional)
  2. Return proper exit status codes
  3. Document runtime dependencies
  4. Log messages using the Init.d functions: log_success_msg, log_failure_msg and log_warning_msg (optional)

To document runtime dependencies you’ll have to define a header on your script. The header is required by the Debian Policy Manual to have a valid LSB script. Make sure the Default-Start and Default-Stop represent the appropriate Run Levels for both. Also, you’ll need to document what facilities need to be present for your script to start and stop. These can be found here.

[shell]

!/bin/sh

BEGIN INIT INFO

Provides: update-dns

Required-Start: $remote_fs $network $syslog

Required-Stop: $remote_fs $network $syslog

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Update DNS at boot

Description: Setup A and PTR records in Active Directory

END INIT INFO

[/shell]

The script below satisfies the other three requirements outlined above. For the purposes of explanation here (and because it’s one I needed to write anyhow) I’m going to write an init script that will automatically update the Dynamic DNS on a Windows Domain using PowerBroker Identity Services.

The actions needed to be provided by the script can be accomplished through the use of a Case statement. In the below example I’ve written the start function and combined the restart and force-reload into a single case (which just runs start). My stop action is empty because my script is just updating DNS, so there is no stop. Finally, I wrote a status action to show how the current DNS entry is defined.

[shell]

Provide logging functions like log_success_msg, log_failure_msg and log_warning_msg

. /lib/lsb/init-functions

[ -f /etc/default/rcS ] && . /etc/default/rcS
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/opt/pbis/bin
PROGRAM=/opt/pbis/bin/update-dns
HOSTNAME=myhost.mydomain.com

test -x $PROGRAM || exit 0

case "$1" in
start)
log_begin_msg "Setting DNS FQDN to "$HOSTNAME"…"
$PROGRAM –fqdn $HOSTNAME
log_end_msg 0
;;
stop)
;;
restart|force-reload)
$0 start
;;
status)
$PROGRAM –show
;;
*)
log_failure_msg "Usage: $PROGRAM {start|stop|restart|force-reload|status}"
exit 1
esac

exit 0
[/shell]

This script is also providing proper exit status codes. If the action is undefined, it will exit with a non-zero and print a usage statement (using log_failure_msg) and if it exits normally it will exit with zero.

Now that you have a valid LSB Init Script, here’s how to get it installed and operational on your system.

First, save it to the /etc/init.d directory and name it whatever you used in the header for the script. In my example I’ve named mine update-dns.

Finally, install it into the the proper run level startup directories using the following command:

[shell]
$ sudo update-rc.d update-dns defaults
[/shell]

This will install the startup and shutdown scripts into the run level directories based on how you defined them in the Default-Start and Default-Stop directives in your header.

To uninstall your script and stop it from executing at system startup and shutdown use:

[shell]
$ sudo update-rc.d update-dns remove
[/shell]

You now have a valid LSB Init Script you can use on your system.