How to setup a cron job in Linux

How to setup a cron job in Linux

Overview

The following article will cover the basics of cron scheduler and showcase couple of examples on how to setup a cron job in Linux. Periodically, you want to perform certain task at specific time, like sending disk space report of server or take database backup every night. It will be really wonderful if you can perform such kind of task automatically rather than repeat it and doing it manually, right?

Linux has many task scheduling packages, but among all the available packages, “cron” is the most wildly used utility available on most of the Linux distributions by defaults. If cron not available on your Linux distribution, then you can easily install it from repository.

Using Cron utility, you can easily execute any command or any shell scripts to run at fixed times, any fix day, dates, or intervals. Cron is very similar to the Windows task scheduling utility.

Cron is a daemon utility that runs in the background and continuously checks the /etc/crontab file and any changes added in the /etc/cron.*/ directories.

You can check them by executing the following commands:

ll /etc/cron.*/

You will also find cronjob for each individual users under /var/spool/cron/crontabs directory.

In this article, you will learn how to set up a cron job on your Linux system and perform some basic task using Cronjob.

Prerequisites

  • A server running any Linux flavours.
  • A root password is configured on the server or sudo privileges

Crontab Syntax

Crontab Syntax is straightforward to understand. Below is the syntax of the crontab:

M H DOM MON DOW USER COMMAND

* * * * * user_name command(s)
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

how to setup a cron job in linux
crontab

Note: Here, you need to replace ‘user_name’ with corresponding user.

Let’s understand the above syntax:

Minutes: It defines Minutes (M) in number format from 0 to 59.

Hour: specified hours (H) in 24 hours format from 0 to 23.

Days of the month (DOM): takes number from 1 to 31.

Months (MON): Months number from 1 to 12.

Days of the week (DOW): Specify day of the week in number from 0 to 7. Here, 0 and 7 consider as Sunday.

Basic Command Of Crontab:

  • crontab -e – Using this command, you can edit or create a new crontab file.
  • crontab -l – Display the list of cronjob. It will show the content of the crontab file.
  • crontab -u – Using this command, you can edit other’s user crontab file.

For example:

crontab -u username -e
crontab -u username -l
  • crontab -i – This will remove the current crontab file of the user, with a prompt before the removal.
  • crontab -r – By executing crontab -r command, you can remove the current crontab file.

Cron job Examples:

Now, let us understand to setup a cron job by examples:

1. Schedule a cron to execute on every 2 minutes

Let’s understand cron by creating a basic shell script that will run every 2 minutes.

Create a shell script file and set execution permission

vim /home/markon/filename.sh

put the following command into filename.sh

#/bin/bash
echo "File_`date +'%Y-%m-%d-%T'`" >> /tmp/file

Now, Save and exit the file and change file permission.

how to setup a cron job in linux
chmod u+x /home/markon/filename.sh

Next, execute the following command to set up crontab:

crontab -e

Then, paste the following line

*/2 * * * * /bin/bash /home/markon/filename.sh

how to setup a cron job in linux
cron job setup

save and exit.

The above cronjob will run every 2 minutes and append file output in /tmp/file.

Verify the cron by cat /tmp/file

Output:

[email protected]:~# cat /tmp/file
File_2022-06-23-16:34:01
File_2022-06-23-16:36:01
File_2022-06-23-16:38:01

how to setup a cron job in linux
cron job execution

2. Schedule a cron to execute every 2 hours

Now, you have shell script file example.sh, and you want to run that script every 2 hours.

To run example.sh script every 2 hours and redirect the standard output to /dev/null, execute the following command:

crontab -e

Then, add the following line:

* */2 * * * /bin/sh /home/markon/script/example.sh > /dev/null

3. Schedule a cron to execute from Monday through Friday, every hour, on the hour from 10:00 AM to 7:00 PM

Next, run example.sh script from Monday to Friday and every hour from 10:00 AM to 7:00 PM by running the following command:

crontab -e

Now, add the following line.

0 10-19 * * 1-5 /bin/sh /home/markon/script/example.sh > /dev/null

The above cronjob will run at 10 AM to 7 PM, from Monday to Friday.

4. Run cron job every day at 11:55 PM

Let’s say you want to take PostgreSQL database backup every day at 11:55 PM every day.

Execute the following command:

crontab -e

Now, add the following line and save the file.

55 23 * * * /bin/sh /home/markon/script/pgdump.sh > /var/log/myjob.log 2>&1

5. execute cron job on first day of the month at 3:00 AM

To run, script example.sh at 3:00 AM every first day of the month, execute the following command:

00 03 1 * * /bin/sh /home/markon/script/example.sh > /dev/null

6. Schedule a cron to execute On Sunday at Midnight

You can execute any script on every Sunday at Midnight by running the following command:

crontab -e

Add the following line:

0 0 * * 0 /bin/sh /home/markon/script/example.sh > /dev/null

7. Run multiple scripts using Cron at specific time

You can run multiple scripts or commands using cron, as shown in the below example:

crontab -e

Add the following line:

00 * * * 7 /bin/sh /home/markon/script/pgdump.sh; /bin/sh /home/markon/script/file_sync.sh

The above cron executes pgdump.sh and file_sync.sh scripts on every Sunday at Midnight.

8. Schedule a cron to execute every week, months or year

Execute example.sh every week using the following command in crontab file:

@weekly /bin/sh /home/markon/script/example.sh

You can run example.sh script, every month, by adding the following line in crontab (crontab -e).

@monthly /bin/sh /home/markon/script/example.sh

To run example.sh script every year, run the following command:

crontab -e

Add, the following line to execute the script yearly:

@yearly /bin/sh /home/markon/script/example.sh

9. Run a script or command at system reboot.

You can set cron that will run automatically at system reboot,

@reboot /bin/sh /home/markon/script/example.sh

Summary

To summarize what we have covered in this article – we reviewed the steps and examples on how to setup a cron job in Linux. In the examples we showcased various use cases how to setup a cron job for automating a task and defined at what time for the task to activate.

From this tutorial, you learned what cron is and basic syntax and use of cron using several examples. Using Cron you can send system health report, email, reports, perform system administrator task and many more.

Thank you for your time…

You may also like...

Leave a Reply

Your email address will not be published.