Friday, June 10, 2011

Simply run a script at start up on FreeBSD or Linux using cron

I had the scenario where a particular program would leave a lock file if it failed to complete the job after a reboot.

So the fix was to remove the lock file after a reboot... Very tedious.

I found the easiest way to remove the lock file on start up was to use cron.

cron has a special built in string which allows you to run something once at start up (from 'man 5 cron'):
Instead of the first five fields, one of eight special strings may
appear:

string meaning
------ -------
@reboot Run once, at startup.

You can test this out by running a test which creates a test file (add this to /etc/crontab):
@reboot cricket touch /tmp/testfile