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

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

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

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad