A Guide To Cron Jobs

Cron is one of the most useful utility that you can find in any Unix-like operating system. It is used to schedule commands at a specific time. These scheduled commands or tasks are known as “Cron Jobs”. Cron is generally used for running scheduled backups, monitoring disk space, deleting files (for example log files) periodically which are no longer required, running system maintenance tasks and a lot more. In this brief guide, we will see the basic usage of Cron Jobs in Linux.

The Beginners Guide To Cron Jobs

The typical format of a cron job is:

Minute(0-59) Hour(0-24) Day_of_month(1-31) Month(1-12) Day_of_week(0-6) Command_to_execute

Just memorize the cron job format or print the following illustration and keep it in your desk.

Cron job format

Cron job format

In the above picture, the asterisks refers the specific blocks of time.

To display the contents of the crontab file of the currently logged in user:

$ crontab -l

To edit the current user’s cron jobs, do:

$ crontab -e

If it is the first time, you will be asked to choose an editor to edit the cron jobs.

no crontab for sk - using an empty one

Select an editor. To change later, run 'select-editor'.
 1. /bin/nano <---- easiest
 2. /usr/bin/vim.basic
 3. /usr/bin/vim.tiny
 4. /bin/ed

Choose 1-4 [1]:

Choose any one that suits you. Here it is how a sample crontab file looks like.

In this file, you need to add your cron jobs one by one.

To edit the crontab of a different user, for example ostechnix, do:

$ crontab -u ostechnix -e

Let us see some examples.

1. To run a cron job at every minute, the format should be like below.

* * * * * <command-to-execute>

For example if the time is 10:00, the next job will run at 10:01, 10:02, 10:03 and so on.

2. To run cron job at every 5th minute, add the following in your crontab file.

*/5 * * * * <command-to-execute>

For example if the time is 10:00, the next job will run at 10:05, 10:10, 10:15 and so on.

3. To run a cron job at every quarter hour (i.e every 15th minute), add this:

*/15 * * * * <command-to-execute>

For example if the time is 10:00, the next job will run at 10:15, 10:30, 10:45 and so on.

4. To run a cron job every hour at minute 30:

30 * * * * <command-to-execute>

For example if the time is 10:00, the next job will run at 10:30, 11:30, 12:30 and so on.

5. You can also define multiple time intervals separated by commas. For example, the following cron job will run three times every hour, at minute 0, 5 and 10:

0,5,10 * * * * <command-to-execute>

6. Run a cron job every half hour i.e at every 30th minute:

*/30 * * * * <command-to-execute>

For example if the time is now 10:00, the next job will run at 10:30, 11:00, 11:30 and so on.

7. Run a job every hour (at minute 0):

0 * * * * <command-to-execute>

For example if the time is now 10:00, the next job will run at 11:00, 12:00, 12:00 and so on.

8. Run a job every 2 hours:

0 */2 * * * <command-to-execute>

For example if the time is now 10:00, the next job will run at 12:00.

9. Run a job every day (It will run at 00:00):

0 0 * * * <command-to-execute>

10. Run a job every day at 3am:

0 3 * * * <command-to-execute>

11. Run a job every Sunday:

0 0 * * SUN <command-to-execute>


0 0 * * 0 <command-to-execute>

It will run at exactly at 00:00 on Sunday.

12. Run a job on every day-of-week from Monday through Friday i.e every weekday:

0 0 * * 1-5 <command-to-execute>

The job will start at 00:00.

13. Run a job every month (i.e at 00:00 on day-of-month 1):

0 0 1 * * <command-to-execute>

14. Run a job at 16:15 on day-of-month 1:

15 16 1 * * <command-to-execute>

15. Run a job at every quarter i.e on day-of-month 1 in every 3rd month:

0 0 1 */3 * <command-to-execute>

16. Run a job on a specific month at a specific time:

5 0 * 4 * <command-to-execute>

The job will start at 00:05 in April.

17. Run a job every 6 months:

0 0 1 */6 * <command-to-execute>

This cron job will start at 00:00 on day-of-month 1 in every 6th month.

18. Run a job every year:

0 0 1 1 * <command-to-execute>

This cron job will start at 00:00 on day-of-month 1 in January.

We can also use the following strings to define job.

@reboot Run once, at startup.
@yearly Run once a year.
@annually (same as @yearly).
@monthly Run once a month.
@weekly Run once a week.
@daily Run once a day.
@midnight (same as @daily).
@hourly Run once an hour.

19. To run a job every time the server is rebooted, add this line in your crontab file.

@reboot <command-to-execute>

20. To remove all cron jobs for the current user:

$ crontab -r

For more details, check man pages.

$ man crontab

At this stage, you might have a basic understanding of what is Crontab and how to create and run a cron job in Unix-like systems.

Crontab syntax generators

As you can see, scheduling cron jobs is much easier. Also there are few web-based crontab syntax generators available to make this job even easier. You don’t need to memorize and/or learn crontab syntax. The following two websites helps you to easily generate a crontab expression based on your inputs. Once you generated the line as per your requirement, just copy/paste it in your crontab file.

1. Crontab.guru

Crontab.guru is dedicated website for learning cron jobs examples. Just enter your inputs in the site and it will instantly create a crontab syntax in minutes.

crontab guru

Crontab guru – A quick and simple editor for cron schedule expressions

This site also provides a lot of cron job examples and tips. Do check them and learn how to schedule a cronjob.

2. Crontab Generator

This has been pointed out by one of our reader Mr.Horton in the comment section below. Crontab Generator is yet another website that helps us to quickly and easily generate crontab expressions. A form that has multiple entries is given in this site. The user must choose all required fields in the form.

Finally, hit the “Generate Crontab Line” button at the bottom.

Crontab Generator

Crontab Generator

In the next screen, the user will see his/her crontab expression. Just copy/paste it to the crontab file. It is that simple.

Crontab generator

Generate crontab entries

Easy, isns’t? Both of these websites will definitely help the newbies who don’t have much experience in creating cron jobs.

Also, there is web-based tool named “Crontab UI” to easily and safely create cronjobs. Have a look at the following link if you’re interested to read more about it.

And, that’s all for now. Hope this was useful. More good stuffs to come. Stay tuned!!


Reference links:





Add a Comment