Cron is a Linux utility that lets you automate tasks on a schedule of your choosing. A Linux cron job is the perfect way to automate backups or other repetitive tasks. Cron comes with all versions of Linux and functions mostly the same across different distributions.
How cron works
Cron works by reading through a number of configurable text files and then reading commands into memory. Every minute the cron daemon crawls a special spool folder on the file system that contains a crontab for each user on the system. When cron runs across a crontab with a scheduled job it loads the job into memory.
Types of Linux cron jobs
There are 2 types of cron jobs. System wide jobs are cron jobs configured through /etc/cron and the directories /etc/cron.hourly /etc/cron.daily /etc/cron.weekly and /etc/cron.monthly These jobs are ran under the root account and have full access to the system. User jobs are cron jobs scheduled by standard user accounts and configured through crontabs. For security reasons it is recommended to schedule your jobs through your own crontab unless the task must be executed as root.
How to schedule a cron job
There are 2 ways to schedule a Linux cron job. The first is to simply drop a shell script into any of the directories mentioned below:
/etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly
Shell scripts in these directories must be executable, owned by root, and adhere to a specific set of filename restrictions. From the man page:
The second way to schedule a cron job (and the preferred way) is to edit your users crontab:
This opens up the crontab for the currently logged in user. Here, all cron jobs scheduled will be executed under the user who created it. You will see a number of lines with the proper crontab format at the bottom. This is where you schedule your job. You can drop in a direct command to run or give the path of an executable shell script. Here are a few examples:
Note: cron uses a 24 hour clock ie military time.
Schedule a job to run once a day at 2:00am:
Schedule a job to run at the 1st of the month:
Schedule a job to wish the system a merry christmas:
Clear the temp files once a week on Friday after everyone has left work:
Make the appropriate edits and then save the file. Don’t worry about reloading or restarting cron. The daemon automatically checks for changes every minute.
Cron is drop dead simple once you understand the format. However, there are a few things that can hang up a cron job if you aren’t careful. Here are a few:
1.) Your scripts aren’t executable
All of your scripts must be executable in order for cron to run them. Go back and make sure all of your cron scripts have the +x permission configured.
2.) Your scripts aren’t owned by root
If you’re dropping your scripts into /etc/cron.hourly or any other cron folder, they must be owned by root or cron won’t run them. Make sure your scripts are owned by root and the proper permissions are set.
3.) Your scripts contain characters that aren’t allowed by cron
Similar to point number 2, your cron scripts won’t run in the /etc/cron.* folders unless they conform to a specific format. It’s usually a period that trips people up. Remove the period and your scripts will magically start running. Refer to the quoted man page above for more information.
4.) Wrong format
It’s easy to make a mistake in your crontab. You might think you’re running a script at one time but the format you gave cron doesn’t match up. Go back and make sure you have a clear grasp of the cron format and understand how to implement it into your crontab.
cron is a picky S.O.B but performs well once you can get him running. The nice thing is, once you have cron configured it will run like a well oiled machine for the rest of your days. Let me know what you think about cron in the comments below.