01. Introduction to Cron crontab

The term cron refers to a time-based job scheduler that triggers commands at set time intervals. It is configured through a simple text file called the crontab. Within this file are a list of cron jobs, each having a command and a time interval in which they are to be run.

Cronjobs are a time-based task scheduler.
Cronjobs are a time-based task scheduler.

Practical applications of a cron job

Cron jobs can be used in any sort of automation task. Here is a short list of tasks you could use for automation.

  1. Clear out items in a temporary folder at the end of every week.
  2. Perform a database dump to backup files every night.
  3. Send out daily notification emails every other morning.
  4. Emails that are sent every second week of the month with monthly progress reports regarding website statistics.
  5. A Python script that makes a reddit bot post to a certain subreddit every night at 7:00pm.

The cron daemon

The cron daemon is a dormant process that gets run whenever encountering a time which it is to execute some command. This daemon is responsible for executing each cron job listed in our cron tabs.

If this is the first time hearing the termin daemon (pronounced DEE-mon), it's simply a background process that answers requests to services.

Crontab and examples

Each job to be performed is stored in a file called the crontab. Here is an example of one might look like.

43 2 * * * echo "It's 2:43am!"
0 8 1,15 * * echo "Run every morning at 8:00am on the 1st and 15th of every month."
*/30 9-12 * * * echo "Run every 30 minutes from 9-12 am."

We'll learn how to decipher each line in the next page. For now, let's learn how to access this crontab with the crontab command.

Crontab command and actions

The main command used to edit your crontab is the crontab command.

1) Editing your crontab

We can edit our crontab by passing in the -e option. This will open the file in our default editor. If you've never opened up the crontab before, you'll get the following boilerplate, with comments guiding you on how to set you a cron.

$ crontab -e
# Edit this file to introduce tasks to be run by cron.
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# For more information see the manual pages of crontab(5) and cron(8)
# m h  dom mon dow   command

2) Listing out current crontab file

To list out all current crontab to standard out, use the -l option.

You may also check if the cron daemon is currently running. The top output shows cron is up and running, while the second line shows the current grep command that was just used.

$ crontab -l
# Current crontab file outputted to standard out
$ ps aux | grep cron
root       787  0.0  0.3  23660  1788 ?        Ss   18:45   0:00 cron
JohnDoe   2685  0.0  0.4  15948  2236 pts/1    S+   19:38   0:00 grep --color=auto cron

3) Remove all crontabs

To discontinue the current crontab, use the -r option. We can pair this with the -i option to prompt the user 'y/Y' before actually removing the crontab.

$ crontab -r

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources