Skip to main content

How do I set up a cron job?

1 - What is a cron job?

A "cron job" refers to a recurring task that is automatically executed on a system (Linux, macOS, etc.) at defined intervals.

Typical use cases for cron jobs include:

  • Backups: e.g., automatic database backups every day at 3:00 AM

  • Cleanup: e.g., automatic deletion of temporary files or old logs every Sunday

  • Updates: e.g., regularly checking for software updates

  • Sending email: e.g., automatically sending newsletters or daily reports

The corresponding "cron" package is pre-installed on most Linux systems.

2 - Structure of a cron job

2.1 - Cron job syntax

A cron job is divided into five time fields, followed by the command to be executed:

Minute

The first field of a cron job represents the minute and determines at which minute of the hour the command should be executed.

  • Values ​​from 0 to 59 can be entered in the "Minute" field.

  • An asterisk (*) means the command is executed every minute.

  • A specific number (e.g., 15) means the command is executed every hour at exactly the 15th minute (e.g., 08:15, 09:15).

  • A forward slash (e.g., */15) causes the command to be executed every 15 minutes (e.g., 08:00, 08:15, 08:30, 08:45, 09:00, ...).

  • A hyphen (e.g., 1-5) executes the command during the first 5 minutes of every hour.

  • Multiple values ​​can be separated by commas (e.g., 5,25,40), indicating that the command should be executed at those specific minutes (e.g., 10:05, 10:25, 10:40).

Hour

The second field of a cron job represents the hour and determines at which hour of the day the command should be executed.

  • Values ​​from 0 to 23 can be entered in the "Hour" field; 0 represents midnight and 12 represents noon.

  • An asterisk (*) means the command should be executed every hour of the day.

  • A specific number (e.g., 8) means the command should be executed at the 8th hour every day.

  • A forward slash (e.g., */2) causes the command to be executed every 2 hours (e.g., 0:00, 2:00, 4:00, ...).

  • A hyphen (e.g., 9-17) executes the command every hour from 9:00 to 17:00.

  • Multiple values ​​can be separated by commas (e.g., 2,14,21), meaning the command should be executed at 02:00, 14:00, and 21:00.

Day (of the month)

The third field in the cron job specifies the calendar day on which the command is to be executed.

  • Values ​​from 1 to 31 can be entered in the "Day" field.

  • An asterisk (*) means the command is executed every day.

  • A specific number (e.g., 4) means the command is always executed on the 4th day of the month.

  • A forward slash (e.g., */2) causes the command to be executed every second day.

  • A hyphen (e.g., 20-25) executes the command within a specified range (e.g., from the 20th to the 25th).

  • Multiple values ​​can be separated by commas (e.g., 5,17,26), meaning the command is executed on all these explicitly listed days.

Month

The fourth field in the cron job specifies the month in which the command is to be executed.

  • Values ​​from 1 to 12 or from JAN to DEC can be entered in the "Month" field (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC).

  • An asterisk (*) indicates that the command should be executed every month.

  • A specific number (e.g., 3) indicates that the command should be executed only in that specific month (in this case: March).

  • A forward slash (e.g., */3) causes the command to be executed every 3 months.

  • A hyphen (e.g., 3-7) causes the command to be executed only within a specific range of months (in this case: from March to July).

  • Multiple values ​​can be separated by commas (e.g., 1,6,12), indicating that the command should be executed only in the specified months (in this case: January, June, December).

Day of the Week

The fifth field in the cron job specifies the day of the week on which the command is to be executed.

  • In the "Weekday" field, you can enter values ​​from 0 to 7 (where 0 and 7 represent Sunday) or the abbreviations SUN through SAT (SUN, MON, TUE, WED, THU, FRI, SAT).

  • An asterisk (*) indicates that the command should be executed on every day of the week.

  • A specific number (e.g., 3) indicates that the command should be executed only on that specific day of the week (in this case: Wednesday).

  • A forward slash (e.g., */2) causes the command to be executed every second day of the week (Sunday, Tuesday, Thursday, Saturday).

  • A hyphen (e.g., 1-5) executes the command within the specified range of days (in this case: Monday through Friday).

  • Multiple values ​​can be separated by commas (e.g., 1,3,5), indicating that the command should be executed on all the specified days (in this case: Monday, Wednesday, Friday).

Command to be executed

In a cron job, the command field follows the time fields. This is where the specific command, script, or program to be executed at the defined times is specified.

2.2 - Cron job examples

Below you will find some example cron jobs.

2.2.1 - every minute

* * * * * /home/user/skript.sh

2.2.2 - every 5 minutes

*/5 * * * * /home/user/skript.sh

2.2.3 - every hour on the hour

0 * * * * /home/user/skript.sh

2.2.4 - every 5 hours

0 */5 * * * /home/user/skript.sh

2.2.5 - every day (at midnight)

0 0 * * * /home/user/skript.sh

2.2.6 - every 5 days

0 0 */5 * * /home/user/skript.sh

2.2.7 - every day at 02:15 AM

15 2 * * * /home/user/skript.sh

2.2.8 - every Sunday at 11:00 PM

0 23 * * 0 /home/user/skript.sh

2.2.9 - only on weekdays (Monday to Friday) at 02:00 AM

0 2 * * 1-5 /home/user/skript.sh

3 - How do I set up a cron job on my internex server?

3.1 - Webspace, Managed WordPress

If your package is a webspace or Managed WordPress plan, you are welcome to submit the desired cron job to us via a support ticket at [email protected]

3.2 - Managed Server, vServer

If your product is a managed server or vServer, you can set up the cron job directly via an SSH connection.

Syntax Support

First, create the desired cron job. You can use the following website for assistance: https://crontab.guru/

Establishing an SSH Connection

Connect via SSH to the server where you wish to set up the cron job.

Opening Crontab / Starting the Editor

Enter the command

crontab -e

to start the configuration. The file will open in your preferred text editor (e.g., Nano).

Adding the Cron Job

Add the desired cron job to the end of the file. Ensure that all five time fields are set correctly.

Saving and Closing

Save the file (using Ctrl+S in Nano) and close the editor (using Ctrl+X in Nano). Successful entry is confirmed by the message "crontab: installing new crontab".

Checking Cron Jobs

Individual cron jobs are stored in a system table. You can view the currently configured cron jobs using the command

crontab -l

Did this answer your question?