schedule cron job to run every hour

Schedule Cron Job Every 1 hour in Linux

Cron jobs are tasks and processes that run automatically at regular intervals of time. Often system administrators need to run certain scripts or applications every 1 hour in Linux. In this article, we will learn how to schedule cron job every 1 hour in Linux.


What is Cron Job?

Every Linux system allows you to schedule automated tasks and scripts to run at regular intervals. They can be minutes, hours, days, weeks, day of the week or even a combination of these. They are commonly used by system administrators to perform administration & maintenance of Linux system such as doing back ups, checking for updates, sending emails, installing patches and so on.


Schedule Cron Job Every 1 hour in Linux

All cronjobs in your Linux system are listed in crontab file. You can open it in a text editor, using the following command.

crontab -e

Each cron job is listed on a separate line and must follow the syntax below.

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

Each cron job consists of 6 space separated fields, with first 5 fields consisting of time-based values and the last field is the command to be run.

The first five time-based fields can accept the following operators.

* – asterisk means every occurrence. If it is placed in first field, it means every minute. If it is placed in second field, it means every hour, and so on.

, – comma allows you to specify list of values. For example, specifying 15,30,45 in first field means that the task will run at minute 15, 30, 45 of every hour.

 – hyphen allows you to specify range of values. For example, 1-5 in first field means the task will run every minute during first 5 minutes of every hour.

/ – slash allows you to specify interval steps. For example */10 in first field means the task will run every 10 minutes.

You can use also the last 3 operators in conjunction such as 1-5,10,30-40/2 in the first field means that every hour, the task will run every minute during the first 5 minutes, then at minute 10, and then every 2 minutes starting from minute 30 till minute 40.

The command used in a cron job can be a shell script or a linux command.

Here are couple of examples of cron jobs

0 0 1 * * /home/user/backup.sh
0 10 * * * service apache2 restart

Now let us look at how to run cron job every hour in Linux.


Run Cron Job Every Hour

Here is the syntax to run cron job every hour in Linux.

0  * * * * command

The above command will run cron job at 8.00, 9.00, 10:00 and so on. If you want to run the command at different time of the hour (e.g. 8.05, 9.05, 10.05, etc.) then change the first field to 5.

5  * * * * command

In this article, we have learnt how to schedule cron job every 1 hour in Linux.

Also read:

How to Send Message to Logged User in Linux
How to Delete User Accounts With Home Directory in Linux
How to Update or Change System Locale in Linux
Linux Grep Binary Files for Strings
Linux Prevent File from Being Modified

2 thoughts on “Schedule Cron Job Every 1 hour in Linux

  1. I am trying to run stress or stress-ng as a cron job that should run every 1 hour and the stress test should last for 45min.

    How do i go about doing this?

    • 1. Open crontab with the following command:
      $ crontab -e

      2. Add the following line to run stress command every 1 hour for a period of 45 minutes.
      0 * * * * stress –timeout 2700 >/dev/null 2>&1

      Save and close the file to apply changes.

      Here is the explanation of cron command. If you want to run a command every one hour you need to have following cron syntax,
      0 * * * * [command] >/dev/null 2>&1

      Now your stress command needs to have a timeout value of 2700 seconds (45 minutes) so that it stops after that time. So use –timeout option for this purpose.
      stress –timeout 2700

      Hope it helps.

Leave a Reply

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