What is the easiest way in Linux to have a script run on startup and another on shutdown?
Create the script in /etc/init.d and run chkconfig myscript on
. See the other scripts in /etc/init.d for examples.
As others have pointed out, this is on a Red Hat based distribution.
See the introductory article http://www.linux.com/feature/114107
Most distros have their own front-end to manage runlevels. Unfortunately there is no standard tool.
For example I use OpenSUSE, where it can by managed by YaST.
This is very distribution-dependent, and also varies with stuff like runlevels, and exactly when during start-up you want your script to run.
But poking around in /etc is probably a good way to figure out how to do it for your particular machine.
The easiest way to make a script run on startup is to add a @reboot entry in root's crontab. (I don't know of any @shutdown rule):
@reboot /path/to/script <arguments>
For debian systems (ubuntu included), the "correct" way is to make an init script in /etc/init.d based on the skeleton file in that directory. Use update-rc.d to make it active (read the man for usage).
If your application does not support daemonizing, pid file handling, etc, you can use the command start-stop-daemon to provide these features for you in your init script.
In Debian or Ubuntu, copy /etc/init.d/skeleton, an example script for starting/stopping daemons, and edit it to your liking, and make it executable:
cp /etc/init.d/skeleton /etc/init.d/mythingy
pico /etc/init.d/mythingy
chmod 755 /etc/init.d/mythingy
That should provide a way for you to start and stop a given program/script. You may want to test your script to make sure that it works as expected:
/etc/init.d/mythingy start
/etc/init.d/mythingy stop
After doing this, use the update-rcd command to make it run at startup/shutdown:
update-rcd mythingy defaults
Unless you know what you are doing, using the defaults is perfectly fine. That should be all you need to do!
You might find these resources helpful:
Ubuntu has /etc/rc.local, a script which you can edit to call anything you need to do on startup.
Gentoo has /etc/conf.d/local.start and /etc/conf.d/local.stop
Must be something similar under Redhat/fedora.
Since you haven't specified which distro, all you can rely on is the information from LSB about init scripts.