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?

You can use the following command to check if 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 distribution 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 where you can 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 we can add a Cron job, let’s see some 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

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

* * 1 12 * command/script

Following entry will run this command/script on every monday, after every 1 minute:

* * * * 1 command/script

OR

* * * * MON command/script

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 can 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 then 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



About the Authors

Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic!

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



Leave a Reply

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