How-To: Using And Testing Crontab
Cron jobs can be scary for new users, and I am testament to that. I was wary about setting cron up at first, but now I have several cron jobs for a myriad of things. I will quickly explain the format of cron, and the common use of ‘crontab’ After this article, I hope editing your “cron jobs” is far less intimidating. Read on for more.
Cron is similar to what Windows users call “scheduled tasks.” Although cron can seem very simple, it has great power. At the top of this post is a simple explanation of each field of a “cron job.” Jobs are separated by line, with each line argument separated by a space. I am not sure how strict using more than one space or TAB is, but I have never had an issue personally. There are some key things to remember though:
- Using ‘crontab -e’ to edit your personal user cron jobs is highly suggested over changing individual “daily,” “monthly,” or otherwise system subfolder files.
- ‘crontab’ uses vi, so be careful of commands to insert (i), append (a), exit current mode (ESC) and more. See vi’s man page for more. You can however change your visual editor by exporting your preferred editor, e.g. ‘export VISUAL=vi’ I do not suggest using a GUI based editor. ‘crontab’ uses the $VISUAL environment variable instead of $EDITOR in most cases, instead of the normal “default” editor setting. Otherwise exporting the $EDITOR variable may work just fine for you, such as ‘export EDITOR=”/usr/bin/nano” ‘
- There are six fields, minute, hour, day of month, month, day of week, and the actual command being used
- List your cron jobs with ‘cronjob -l’
If you want to delve into using non-standard numbers and options, here is a list of some of the finer tuning options of crontab:
- ‘*’ will use all values. For example, any minute of the hour.
- ‘-‘ will use contiguous values. For example ‘1-5’ for the month field will specify the job be run in that range of months
- ‘,’ will allow you to specify multiple non-contiguous values. For example, you could use ‘1,25’ to specify the job run only on the first and 25th of the month.
- ‘/’ will allow you do specify the step modifier value. This is sued to indicate the unit of increments between values.
- For example, ‘*/15’ will signify the job should be run every “0th, 15’th, 30th, and 45th” minutes of the hour. Think of it as an incremental, repeating option.
- Please note crontab uses a 24 hour format, so please familiarize yourself with such.
- Using ‘crontab -e’ edits cron jobs for the current logged in user.
- You can also use crontab specific variables instead of the 5 field options, such as @yearly, @daily, @hourly, and @reboot.
- For other interesting crontab options , please see this post.
The easiest way to test cron is to echo some garbage to a text file. There are other methods, but this has always proven to be the easiest, cross platform way I know (Unix/BSD/Linux and so on).
* * * * * echo "crontab successful" > /home/username/crontest.txt
Then after about 2 minutes, check that text file. If all is well, you should see your sample text. Using ‘>>’ will append text, allowing you to confirm multiple entries. Alternatively you could output the contents of the ‘date’ command to the file as well. For some interesting and useful crontab examples, please see this post, and keep in mind it is only for reference as your usage may be entirely different.
If you have any questions or comments, please leave them below.