Server Reboots
Intro to rc.local
Everyone running @reboot cron scripts has lead to huge server load at startup. To relieve this server stress dgibbons? has come up with a new method. Run startup scripts in sequence instead of all at once. Besides the server load issue it is also more difficult to test exactly what will happen with cron jobs because cron does not load environment variables as are in place when running programs from a terminal. “rc.local” is simply a shell script that starts all of the programs that you need to have run at startup. It’s easy to set this up.
How to create an rc.local script
Check for existing files
Check to see if you already have an etc directory, and maybe even an rc.local file. You could have one of the accounts where we already did this part for you. This will probably be standard practice in the near future.
$ ls ~/etc
rc.local
If you see something like that then skip ahead to the part about editing your rc.local script.
If instead you see…
$ ls ~/etc
ls: /var/www/virtual/<my.domain>/etc: No such file or directory
Then you need to make an etc directory.
In the previous example <my.domain> will be replaced with the domain name you have setup with PLANET ARGON.
Create a etc directory
$ mkdir -v ~/etc
mkdir: created directory `/var/www/virtual/<my.domain>/etc'
Create a minimal script
Next create a shell script with the name rc.local in your new etc directory.
$ echo '#! /bin/sh' > ~/etc/rc.local
$ chmod +x ~/etc/rc.local
$ $EDITOR ~/etc/rc.local
If you have an EDITOR environment variable setup the last command here will load your new rc.local script in your favorite editor. See EnvironmentVariables? for more on environment variables. Alternatively replace $EDITOR with the name of your favorite editor in that command.
Edit your script
Once your editor is open you should only see #! /usr/bin go to the next line, and add commands as you would run them from the terminal. One per line. Lines other than the first one that begin with # are comment lines, and you can use these to remind yourself what a command is doing. Blank lines are ignored.
#! /bin/sh
# This script starts programs after a server reboot in a way that
# prevents high server load at server startup.
# The next few lines starts my rails application
( cd ~/rails && ./script/server -d lighttpd )
mongrel/nginx example _mongrel_nginx_rc.local
Remember to save your changes after you edit your script. It is safe to save a backup of your script before editing it in your etc directory. To do that simply copy your rc.local file.
cp -a ~/etc/rc.local ~/etc/rc.local.backup
Test your script
Nobody wants to go to the effort of creating a script to run their application only to find out that they have a bug in the scripts, and their application doesn’t actually run when the server restarts.
Check running applications
First take a look at what applications you have running using the ps? command. It could be easy to forget about something you need to have running.
$ ps x -Ho pid,args
PID COMMAND
30279 -bash
1521 ps x -Ho pid,args
11298 lighttpd -f config/lighttpd.conf
11299 ruby /var/www/virtual/<my.domain>/typo/public/dispatch.fcgi
11300 ruby /var/www/virtual/<my.domain>/typo/public/dispatch.fcgi
With this view it is sorted so that processes that are children of other processes are indented. So it is clear that lighttpd started both ruby processes. Only that process needs to be restarted. The command line that shows up with this process view is not nessisarily the command that should be used to start it though. It is entirely possible that it was started from an exec? call rather than started as a subprocess. So if you see something different from what you used to start your application that probably isn’t something to worry about.
Shut down servers
To test if the rc.local script can successfully start your servers it is necessary to shut them down first. In this example that is simple.
$ kill 11298
Use ps to check to see if it really got shut down.
Run rc.local
$ sh ~/etc/rc.local
Use ps again to see if everything started back up as expected.