The Basics of CRON and Linux Automation
by Mark Rais, senior editor reallylinux.com.
This beginner introduction to the Linux/Unix CRON process and tables allows even a novice to automate commands that need to be run repeatedly.
The benefits for automating tasks and commands is obvious. System administration becomes far easier, and you can perform multiple tasks or maintenance without having to be present.
Now, let's get started with the basics of cron and at.
ONE TIME AUTOMATED TASK
If you have no need for a command to be run repeatedly, then it will save you a whole lot of work to just use the at command.
For example, running as root you type:
at midnightOnce you press enter, you are taken to the at command prompt. Beside the at> prompt type your command. For example:
at> who > who.outYou are allowed one command per line. Press enter to go to the next line. When done, press the Ctrl and D keys simultaneously.
X 2006-05-26 00:00 a yournameThe X represents the number assigned to your at command.
atrm XReplace the X with the number of your command. Note that the atrm is the at remove command.
Minute (0-59) Hour (0-23) Day of Month (1-31) Month (1-12 or Jan-Dec) Day of Week (0-6 or Sun-Sat) CommandYou do not need to specify each item. If you want to specify every month you simply use a * to denote every occurance. Using this same sequence above, and the option of * for every occurance, here is a simple example:
* * * * 5 whoThe above line will run the who command every minute, of every hour, of every month, for each Friday. The 5 being in place of Day of Week.
5 * * * * whoThe above line will run the who command on the fifth minute, of every hour, of every month, for all days of the week. The 5 being in place of Minute.
0 0 * * * whoThe above line will run the who command at midnight, of every month, for all days of the week. The 0 is in place of Hour and minutes, and represents midnight (0-23 of a 24 hour clock).
*/15 * * * * whoThe above line will run the who command every 15th minute, of every hour, of every month, for all days of the week. The /15 being in place of frequency of Minutes.
crontab -e2. You will be switched to an unusual looking interface, also called the Vi Editor. Press the i key to Insert your cron command. 3. Now type the following:
*/15 * * * * yourcommandPlease replace yourcommand with an actual command line you wish to run.
:wqYou should see this:
crontab: installing new crontabYou have just setup a new cron function that will run a command every fifteenth minute of every hour of every day.
*/10 * * * * wget -q -O /dev/null http://example.com/moodle/admin/cron.phpHere is exactly every item that I typed:
crontab -eI used the crontab -l command to list the entry to ensure it was included in the right way.
*/10 * * * * wget -q -O /dev/null http://example.com/moodle/admin/cron.php
Mark Rais serves as senior editor for reallylinux.com, promotes Open Source to organizations and government leadership in USA, Asia and Africa, and has written numerous Linux books, including Linux for the Rest of Us.
Linux is a registered trademark of Linus Torvalds. IBM, PC-DOS, and OS/2 are the registered trademarks or trademarks of International Business Machines. Microsoft, Microsoft Vista, Microsoft Windows are all trademarks or registered trademarks of Microsoft Corporation both in the United States and Internationally. All other trademarks or registered trademarks in this opinion piece belong to their respective owners.