Setting Up a Cron Job in Linux

Crontab is used for running specific tasks on a regular interval. Crontab is very useful for routine tasks like scheduling system scanning, daily backups etc. Crontab executes jobs automatically in the backend on a specified time and interval. In this tutorial, you will learn to setting up a cron job for the first time.

Basic Cron Syntax

# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]

Parts of a cron command

There are four main parts to a cron command.
Timing     Execute Command     Path to script     Output
* * * * *     /usr/bin/php     /var/www/html/crontest/cron.php     > /dev/null 2>&1

    Timing – set the minutes, hours, days, months, and weekday settings (more below).
    Execute – the cron job needs to call upon PHP to run, which is located at /usr/bin/php.
    Path to script – the full path of the file you plan to run.
    Output – (optional) you can write the output to a file or discard it – > /dev/null 2>&1 will discard.

Currently we have nothing any detail of cron job on following path:

# cd /var/spool/

There is nothing in this directory.

# cd cron && ls

Setting Up a Cron Job

By default, it will edit crontab entries of current logged in user. To edit other user crontab use command as below

crontab -u username -e

See how we can scheduled a cron job for every minute.

# crontab -e
*/1 * * * * echo "hello world" > /home/asif/file.txt

no crontab for root - using an empty one
crontab: installing new crontab

# crontab -l
*/1 * * * * echo "hello world" > /home/asif/file.txt

After scheduling the cronjob we can see the below mentioned output on /var/spool/cron/root path.

# cat /var/spool/cron/root
*/1 * * * * echo "hello world" > /home/asif/file.txt

Go to logs to see who has applied the cronjob on which time on /var/log/cron path.

# tail -f /var/log/cron
Feb 12 17:41:00 localhost crontab[7841]: (root) BEGIN EDIT (root)
Feb 12 17:41:09 localhost crontab[7841]: (root) REPLACE (root)
Feb 12 17:41:09 localhost crontab[7841]: (root) END EDIT (root)
Feb 12 17:42:01 localhost CROND[7874]: (root) CMD (echo "hello world" > /home/asif/file.txt)

After executing the cron job we can see the output on below mentioned location.

# cat /home/asif/file.txt
hello world

So if I modify cronjob to append the command output into file.

# crontab -e
*/1 * * * * echo "hello world" >> /home/asif/file.txt

The output would become like this. Cron will run after every minute and output will be appended into file.

# cat /home/asif/file.txt
hello world
hello world
hello world
hello world

Conclusion

There are more tutorials that will go into deeper explanation of cron jobs, but this is a simple article to just get familiar with the concept.

Avatar photo

Asif Khan

Responsible and proactive professional with more than 13 years of experience in IT systems, open source software applications, DevOps, Linux systems, and cloud operations. My main goals are to automate things, keep them safe, and make sure they are strong. I am very good at planning and building the infrastructure for services that people really want. I was drawn to the fast-paced world of cloud computing because it has resources that can be scaled up or down as needed. One of my best skills is being able to use a lot of different DevOps tools to set up, release management, and microservices ecosystems, as well as for provisioning, orchestration, and configuration management.