RSS LinkedIn Twitter

Apache mod_proxy_balancer Running Scripts at Startup/Shutdown : Part 2

February 4th, 2010 Categories: Apache Web Server, client/server, Shell Scripting

I think I have to break the self-registration into two posts, it took a lot longer than I expected last night. This post deals with getting scripts to run at startup and shutdown on Linux. I did this on Fedora, I imagine the process would be similar on Ubuntu, etc.

This is actually the last thing I did, but I’m ordering it first because it’s a bit more general. My goal was to have a script that I could call as follows to register with my load balancer manually:

service lb_register start|stop|status|restart|reload|force-reload

And then also to register this service to be called at startup and shutdown, to register and deregister with the load balancer respectively.

Let’s start with creating the script. You can find tutorials for creating shell scripts just about anywhere on the web. I found it easier to take an existing script from the /etc/init.d/ directory, clear it out and start from that shell. Ironically the one that I chose was the startup script for Pound, another load balancer (a very good one, I might add). It shouldn’t matter which one you use, since all we’re really after is the format. The basic format is essentially the same for all of them: you have your function definitions at the top, and a case statement at the bottom.

The cases match the option parameters (start and stop are very common, as well as the others listed above in the callout). And the case blocks generally call one or more of the functions. I won’t list the case statement in this post as they are all very similar. There are some important things to note in the function definitions, though.

Here is the definition for my start() function:

start() {
   echo -n $"Registering with the load balancer:"
   ./usr/local/bin/register_with_lb.pl
   touch /var/lock/subsys/lb_register
}

You can see that I call a perl script (which presumably registers with the load balancer). Then I touch a file in the /var/lock/subsys/ directory. This is very important if you want a script to automatically run at shutdown. At shutdown the rc script will check for the presence of this file, if it is there it will call stop(). If it is not there, it assumes the service is stopped already and will not call stop() on the service.

My stop function:

stop() {
   echo -n $"Deregistering with the load balancer: "
   ./usr/local/bin/deregister_with_lb.pl
   rm -f /var/lock/subsys/lb_register
}

This calls the deregister perl script, and then removes the lock file for good housekeeping. There is one more thing about the script itself I want to mention before moving on to the registration. There is a line in the comments of most of the scripts (if not all) in /etc/init.d/ that will look something like this:

# chkconfig: - 85 10

or

# chkconfig: 2345 55 15

These are directives to chkconfig (the command we’ll be getting to in a moment) on how to set this script up. The – or the first grouping of numbers deal with the run levels. It tells chkconfig what levels this script will be turned “on”. It is assumed that it will be “off” in the omitted ones.

The second number is a startup priority, different scripts can have different startup priorities in case there are prerequisite dependencies. In the two examples above, the script containing the bottom directive would run before the script containing the bottom one. The last number is the shutdown priority, which is the same thing only during the shutdown process.

Once you have your script, you will need to run the chkconfig command. To add your script to the startup process:

chkconfig --add lb_register

And should you need to remove it:

chkconfig --del lb_register

If you’ve done everything correctly, register_with_lb.pl will be called at startup, and deregister_with_lb.pl will be called at shutdown. If it doesn’t, check that you’re touching the pid file, and that your scripts are executable. You will also be able to make calls like this to deregister and register manually:

service lb_register start
service lb_register restart
service lb_register stop

I should mention a few sites that helped me out quite a bit:
Linux Init Processes
Introduction to BASH Programming
An interesting script for creating new scripts

Tags: , , , , , , ,
No comments yet.

Leave a Comment

*