Wednesday, February 22, 2012

Executing programs at system startup

Some things need to be run before a user log in.  Could be a check for updates on an imaging server, clean up of files, a splash screen with your logo on it, whatever.  Here’s how to do it for Windows, OS X, and Gnu/Linux.

Gnu/Linux

Copy your shell script to the /etc/init.d directory.  Each distribution does things a little differently, so check out one of the other scripts in that directory to see what options are typical, i.e. start, stop, restart, and how to implement them.  It may be easier to copy an existing one and then just have it call your script instead of whatever it is supposed to call.  If you don’t need that functionality, don’t bother.  No sense in making work for yourself.
Next, go to the /etc/rcS.d directory and create a symbolic link to the script you put in /etc/init.d.  Name the link S##yourscriptname, where ## is a number representing the order in which it will be run.  For example, if your script depends on some nfs share, then you would use a number higher than the mountnfs script.
Just remember to do all the stuff you normally would do:  make the script executable, change the ownership to root if necessary, etc.  Oh, and test it first, then test it again.  You don’t want to mess up the boot sequence and then have to boot in to single user and fix it.

OS X

You will need to be root to do many of these steps.  If you are VERY careful, you can type ‘sudo -s’ in Terminal, enter your admin password and you’ve got yourself a root shell.  If you aren’t familiar with running as root, search around the web for all the bad things that can happen, then proceed with caution.  You’ve been warned, so don’t write to me if you hose your system.  Copy a folder from /System/Library/StartupItems to /Library/StartupItems.  Apple-installed stuff goes in the /System/Library, all of your administrative stuff should go in /Library.  Pick one that is similar to what your script will do, i.e., if your script uses some network stuff, copy something that uses network stuff.  Change the name of the folder to the name of the script you intend to run from here.  If there is a Resources folder, remove all of the language folders except for the ones you intend to use.  In the Localizable.strings folder and change the key and string that begin with “Starting something” to “Starting yourscript”.  Edit the StartupParameters.plist file so that the values make sense for your script.  I know that this is a vague statement, but your script may differ greatly from whatever example I may provide.  You’ll have to make changes to the Description, Provides, Requires, Uses, OrderPreference, and Messages.  It might be an XML file or a text file, but the keys are the same.  Edit and rename the existing script in the file or copy in your own.  Make sure it’s executable and owned by root.  If you set the group ownership to admin and give group execute privileges, then your admin users can run the script while the system is running.

Windows

Windows startup and shutdown scripts are run after networking has been established and before networking is shut down, respectively.  Place your script in the appropriate folder, c:\\WINDOWS\system32\Group Policy\Machine\Scripts\Shutdown|Startup.  Run gpedit.msc from the Run prompt, expand Computer Configuration -> Windows Settings, then click on Scripts (Startup/Shutdown).  Double click on the one you want to add your script to, click add in the dialog box that appears, then browse to your script.

Resources

http://www.microsoft.com/technet/archive/community/columns/tips/2kscript.mspx?mfr=true
http://www.kernelthread.com/mac/osx/arch_startup.html
http://www.osxfaq.com/Tutorials/LearningCenter/HowTo/Startup/index.ws
http://www.linuxjournal.com/article/7393

No comments:

Post a Comment