Atlantic.Net Blog

How to Schedule Cron Jobs with Crontab

Cron is a time-based job scheduling daemon in Linux-based operating systems. It is used to schedule specific tasks to run periodically at fixed times, dates, or intervals. It is very similar to the Windows task scheduling utility and is very useful for routine tasks including:

  • Daily backups
  • System scanning
  • Automated system maintenance

The cron daemon runs in the background and continuously checks the /etc/crontab file, and /etc/cron.*/ directories.

Crontab Syntax

The basic syntax of crontab file is shown below:

M H DOM MON DOW USER COMMAND

* * * * * root 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)
  • Minutes (M) specified as a number from 0 to 59.
  • Hours (H) specified as numbers from 0 to 23.
  • Days of the month (DOM) specified as numbers from 1 to 31.
  • Months (MON) specified as numbers from 1 to 12.
  • Days of the week (DOW) specified as numbers from 0 to 7, with Sunday represented as either/both 0 and 7.

Crontab Commands

Some commonly used crontab commands are shown below:

  • crontab -e : Used to edit or create a new crontab file.
  • crontab -l : Used to display the content of the crontab file.
  • crontab -i : Used to remove the current crontab file with a prompt before removal.
  • crontab -r : Used to remove the current crontab file without prompt.
  • crontab -u : Used to edit other user’s crontab file.

1. Schedule a cron to execute at 10 AM Daily task

If you want to have a script named /opt/test.sh run every day at 10 AM, run the following command:

crontab -e

Add the following line:

0 10 * * * /bin/sh /opt/test.sh

2. Schedule a cron to execute on every 5 minutes

To run a test.sh script every 5 minutes, redirect the standard output to /dev/null, and send errors to a specified email address, run the following command:

crontab -e

Add the following line:

[email protected]

*/5 * * * * /bin/sh /opt/test.sh > /dev/null

3. Schedule a cron to execute on every day, every hour, on the hour from 10 AM to 6 PM

To run a test.sh script every day, every hour, on the hour from 10 AM to 6 PM, run the following command:

crontab -e

Add the following line:

00 10-18 * * * /bin/sh /opt/test.sh

4. Schedule a cron to execute at 10 AM on the first of every month

To run a test.sh script at 10 AM on the first of every month, run the following command:

crontab -e

Add the following line:

00 10 1 * * /bin/sh /opt/test.sh

5. Schedule a cron to execute at 10 minutes after midnight and every three hours after that, every day

To run a test.sh script at 10 minutes after midnight and every 3 hours after that, every day, run the following command:

crontab -e

Add the following line:

10 0-23/3 * * * /bin/sh /opt/test.sh

6. Schedule a cron to execute on specific days

To run a test.sh script each Monday and Friday at 6 PM, run the following command:

crontab -e

Add the following line:

0 18 * * mon,fri /bin/sh /opt/test.sh

7. Schedule a cron to execute multiple scripts

To run a test.sh and test1.sh script at a 2-hour interval, run the following command:

crontab -e

Add the following line:

0 */2 * * * /bin/sh /opt/test.sh; /bin/sh /opt/test1.sh

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

To run a test.sh every week, add the following line:

@weekly /bin/sh /opt/test.sh

To run a test.sh every month, add the following line:

@monthly /bin/sh /opt/test.sh

To run a test.sh every year, add the following line:

@yearly /bin/sh /opt/test.sh

Conclusion

In the above guide, you learned what cron is and how to use it, with several examples. Start using cron jobs to automate daily tasks today with VPS hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year