How to use Cron jobs

A Cron job is a job that runs automatically after a fixed interval of time. A Cron job can either be a command or a shell script that you want to run periodically at a specified time.

Once you schedule it, it will run automatically at a specified time, day, and month.

How does the Cron job work?

To add any job to your system, you can use the Crontab. Crontab stands for Cron tables.

Crontab is a file that contains instructions to run jobs using the Cron daemon. A daemon is a program that runs in the background. The name of daemons usually ends at the letter d.

The Cron daemon starts automatically from /etc/init.d at system startup.

You can use the following command to check the daemon status:

systemctl status cron

How to install Cron?

In addition, you can use the following command to check if it is installed in your system or not:

dpkg -l cron

Before installing it to any distribution of Linux, first, update your system by using the update command.

During the installation or update, if any command does not work then run it by using sudo before the command.

Run the following command to install it on Debian-based distributions like Ubuntu, Linux Mint, or Kali Linux:

apt-get install cron

OR

apt install cron

Run the following command on Fedora-based distributions like CentOS and Red Hat Enterprise Linux (RHEL):

yum install cronie

On some Fedora-based distributions, the following command is also used to install it:

yum install vixie-cron

Examples:

The syntax of the Crontab entry is as follows:

minute hour day_of_month month day_of_week command/script

0-59 0-23 1-31 1-12 1-7

Now, before we run any job, let’s see how to add a Cron job. As mentioned earlier, we use Crontab to add any Cron jobs.

Run the following command to add a Cron job entry:

crontab -e

The option -e is used to edit the Crontab. When you run this command, you will see a file opened in an editor to add an entry for the job.

For example, the following entry will run the date command after every minute.

* * * * * date

After you save and exit from the editor, this Cron job will be added to the system. Now, you will see that the date command is running after every minute.

How to print the result of a Cron job in a specific terminal?

You may not see the results of the above Cron job in your terminal. In that case, check the name of your terminal tab and print results in that terminal.

You can check the name of your terminal tab by using the following command:

tty

Now the Crontab entry will become:

* * * * * date > /dev/pts/0

Where /dev/pts/0 is the name of the terminal tab in my case, you might have a different name depending on the terminal tab opened.

How to list all the cron job entries?

You can use the following command to list all the Cron jobs that you have added:

crontab -l

How to remove all the Cron job entries?

Run the following command to remove all the cron jobs at once:

cronjob -r

If you use the -i option with -r then it will ask you whether you want to delete the cron jobs for this user e.g.

crontab -i -r

How to store the result of a Cron job in a file?

We can add the following Crontab entry to store the results of a job in a file instead of showing it in the terminal e.g.

* * * * * date > fileName

Now that you know how to add a Cron job let’s see more examples using different syntax.

Following Crontab entry will print Hello World at every 30th minute:

*/30 * * * * echo “Hello World”

Use the following entry to run the pwd command at the 15th minute of each hour:

15 * * * * pwd

The following entry will run this script every 7th day of the month every minute:

* * 7 * * bash scriptName.sh

You can also make your script executable and then add the entry without specifying the name of the shell e.g.

chmod +x scriptName.sh

Now you can add the following entry:

* * 7 * * ./scriptName.sh

The following entry will run this command/script on December 1, after every 1 minute:

* * 1 12 * command/script

The following entry will run this command/script every monday, after every 1 minute:

* * * * 1 command/script

OR

* * * * MON command/script

The following entry will run this command/script at 8 AM daily:

0 8 * * * command/script

How to add Crontab entries for other users?

In the above examples, we saw how we could add jobs for the logged-in user. However, we can also add the jobs for other users available in your system.

Suppose there is a user named davos in your system. Run the following command to add Cron job entry for this user:

crontab -u davos -e

Similarly, you can also list the jobs for this user using the following command e.g.

crontab -u davos -l

Run the following command to remove all the jobs for this user:

crontab -u davos -r

If any of the above commands do not work, try running them by using sudo before them.

You can go through the following manual pages to learn more about Cron daemon and Crontab.

man cron

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.