Hack 86 Create Your Own Startup Scripts


figs/beginner.gif figs/hack86.gif

Ensure your favorite installed applications start at boot time.

Some ports are nice enough to create their own startup scripts in /usr/local/etc/rc.d when you install them. Unfortunately, not all ports do. You may wonder why you're not receiving any email, only to discover a week later that your mail server didn't start at your last bootup!

In those cases, you'll have to write your own startup script. Fortunately, that's easy.

8.11.1 Was a Script Installed?

Every port comes with a packing list of installed executables, files, and manpages. To see if a particular port will install a startup script, search its pkg-plist for the word rc. Here, I'll check the packing lists for the stunnel and messagewall ports:

% grep -w rc /usr/ports/security/stunnel/pkg-plist etc/rc.d/stunnel.sh.sample % grep -w rc /usr/ports/mail/messagewall/pkg-plist %

Use the -w switch so grep searches for the full word rc, not just words containing those two characters. If there isn't a startup script, as is the case for messagewall, you'll just get your prompt back.

If the startup script ends with .sample, you'll need to copy it to a new file without that extension. This is often the case with applications that expect you to change the sample configuration file to suit your system's requirements.

Also, note the relative path. The packing list knows that, by default, the files installed by a port will start with the prefix /usr/local. That is, in the previous example, you'll find stunnel's startup script in /usr/local/etc/rc.d, not in /etc/rc.d.

The converse is also true. If you don't want an installed application starting itself at boot time, either remove the .sh extension from its startup script or use chmod -x to make it nonexecutable.


8.11.2 Creating Your Own Startup Script

Suppose you'd like to have messagewall start automatically at boot time. That means you'll need to write a script. Fortunately, you don't have to reinvent the wheel, as all startup scripts follow the same pattern. If you've installed some applications, you most likely already have startup scripts populating /usr/local/etc/rc.d. If you don't, use the template startup script from the Handbook:

#!/bin/sh echo -n ' FooBar' case "$1" in start)         /usr/local/bin/foobar         ;; stop)         kill -9 `cat /var/run/foobar.pid`         ;; *)         echo "Usage: `basename $0` {start|stop}" >&2         exit 64         ;; esac exit 0

This script starts a generic application named foobar. When you copy the template, copy it to /usr/local/etc/rc.d with the name of the application followed by a .sh extension. In my case, I'll call the file /usr/local/etc/rc.d/messagewall.sh.

Next, replace the word foobar with the name of the application. Change these three lines to reflect the application's name:

echo -n ' Messagewall' /usr/local/bin/messagewall kill -9 `cat /var/run/messagewall.pid`

Remember to double-check the location of that executable, as some ports instead install to /usr/local/sbin or /usr/X11R6/bin:

% which messagewall /usr/local/bin/messagewall

Occasionally, a port will install its main binary with an odd executable name. For example, the executable for netcat is not netcat. In that case, searching the packing list will reveal all:

% grep bin /usr/ports/net/netcat/pkg-plist bin/nc

Just remember that there's a /usr/local in front of that bin/nc.

8.11.3 Testing the Script

Once you've saved your changes, make the script executable with chmod +x. Then, see if it works:

# /usr/local/etc/rc.d/messagewall.sh  MessagewallUsage: messagewall.sh {start|stop} # /usr/local/etc/rc.d/messagewall.sh start <snip startup messages>

Pay attention if you receive any error messages. Often they indicate a typo in the application's configuration file. Address those and ensure you can successfully start the application.

Once the application successfully starts, make sure you can stop it:

# /usr/local/etc/rc.d/messagewall.sh stop <snip error message regarding PID>

Some applications, like this one, don't record their PID in /var/run, so your script will produce an error instead of stopping the application. Most of these applications take over your prompt when you start them, so you can simply return to the terminal (or background process if you started it as such) and press Ctrl-c to end the process. This isn't the cleanest of procedures, but it is effective nonetheless.

8.11.4 Hacking the Hack

If you're using FreeBSD 5.1 or higher, you might want to experiment with writing your own scripts using the new rc.d structure inherited from NetBSD. As of this writing, /etc/rc.d, or the collection of system scripts, uses this structure. In the future, /usr/local/etc/rc.d will likely migrate to this scripting style.

The new structure adds other commands, such as status and reload, so your scripts can do more than start and stop.

When writing your own scripts, add these lines to your template:

. /etc/rc.subr name="foo" command="/usr/local/bin/${name}" pidfile="/var/run/${name}.pid" your stuff here load_rc_config $name run_rc_command "$1"

The first line is mandatory, as it calls the needed subroutines. Your script will also require the last two lines. Next come three variables that every script should include. There are dozens of other useful variables, so read through the scripts in /etc/rc.d/ for ideas.

I also find NetBSD's packages list useful (see ftp://ftp.netbsd.org/pub/NetBSD/packages/pkgsrc/README-all.html). If you select a port and click on its history then files, you can look for existing scripts. These scripts are written in the NetBSD rc.d style, so you'll have lots of examples to browse.

Don't include the rcvar= variable in your local scripts. This is for system daemons that can be enabled and disabled using rc.conf variables.


8.11.5 See Also

  • man rc.subr

  • The startup scripts section of the FreeBSD Handbook (http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/configtuning-starting-services.html)



BSD Hacks
BSD Hacks
ISBN: 0596006799
EAN: 2147483647
Year: 2006
Pages: 160
Authors: Lavigne

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net