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!
Special Reports: DevOps
Have projects in development that need help? Have a great development operation in place that can ALWAYS be better? Regardless of where you are in your DevOps process, Linux Journal can help!
With deep focus on Collaborative Development, Continuous Testing and Release & Deployment, we offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, advice & help from the experts, plus a host of other books, videos, podcasts and more. All free with a quick, one-time registration. Start browsing now...