rc.local, Cron Style
Occasionally as seasoned Linux users, we run across simple things we
never knew existed—and are amazed. Whether it's tab autocompletion,
sudo !! for when you forgot to type sudo or even recursive file listing
ls, the smallest tricks can be so incredibly useful. Not long ago,
I had one of those moments.
Most people know rc.local is the file where you put commands you want to have start on system boot. Sometimes the rc.local script is disabled, however, and it doesn't work. It also can be difficult to remember the syntax for starting a particular program as a specific user. Plus, having a long list of programs in rc.local can just become ugly. Little did I know, cron supports not only periodic execution of commands, but it also can start programs when the system starts as well!
A normal crontab entry looks like this:
* * * * * /usr/bin/command
That runs the command every minute. There are countless variations to get very specific intervals, but until recently, I didn't know there were options to the five fields. The following is a crontab entry that runs a command every hour on the hour:
And, there are many more:
most interesting for this article,
@reboot. If you have a crontab entry
it will execute when the system starts up, with the ownership and
permission of the person owning the crontab! I researched a lot to
make sure it wasn't just on reboot, but also on a cold boot—and yes,
@reboot terminology just means it runs once when the system first
boots. I've been using this as a quick hack to start programs, and it
works amazingly well.
I know 99.9% of you already knew this juicy bit of info, but for that .1% who have been living in the dark like me, I present you with a sharp new arrow for your system administrator quiver. It's a very simple trick, but all the best ones are!
Practical books for the most technical people on the planet. Newly available books include:
- Agile Product Development by Ted Schmidt
- Improve Business Processes with an Enterprise Job Scheduler by Mike Diehl
- Finding Your Way: Mapping Your Network to Improve Manageability by Bill Childers
- DIY Commerce Site by Reven Lerner
Plus many more.