var img = document.createElement('img'); img.src = "https://easystat.de/piwik.php?idsite=13&rec=1&url=https://docs.servinga.cloud" + location.pathname; img.style = "border:0"; img.alt = "tracker"; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(img,s);
Skip to main content

What is a Cronjob?

A cronjob is a time-based task that runs in a defined interval. In Unix-like operating systems, crontab is commonly used to schedule said cronjobs.

Simply put, this handy utility helps you automate tasks by running scheduled jobs at predefined times. Two of the most common use cases for cronjobs are e.g. running regular backups of your data or cleaning up old files in a directory you don't need anymore.

Cronjobs that are being executed by crontab are configured in a file with exactly that name. Usually you can find it under /etc/crontab. Also, you can create new files in /etc/cron.d to avoid that your crontab file gets too bloated and messy.

While there's a systemwide crontab file, each user also has its own crontab. To easily edit the crontab of a user, sign in as the user you want to edit the crontab for and run the following command:

crontab -e

In case you want to edit it with an editor that is not you default one, you can adjust the EDITOR environment variable for the command call.

EDITOR=vi crontab -e

Of course, in order to edit the systemwide crontab or to add or edit a task-specific file, you can simply use an editor of your choice. If you're new to Linux and don't know how to edit a file, you can learn it in our tutorial on that topic.

Syntax

When editing a crontab file, it's important to know that each scheduled cronjob is defined in one line. The beginning of the line determines when and how often a cronjob is being executed. A so-called cronjob expression consists of 5 fields, each separated by a Space character.

First of all we will look at the syntax:

* * * * *
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Sunday = 0, Monday = 1, ...)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
info

Cronjob expressions are a mighty tool to master. To learn more about them, we recommend using an online tool like cronjob.guru which is the best and most user-friendly way to discover and explore all kinds of different cronjob expressions and to find the one you want to use.

This tool also generates you a list of the next executions your job will run at which is very handy to verify that your cronjob expression will work as intended.

After the cronjob expression, there's the command that should be executed. Usually, it's a good idea to specify the full path to the command. A complete cronjob definition in your crontab will look something like this:

30 4 * * * /opt/run-backup argument-1 argument-2

This example will call the binary /opt/run-backup with argument-1 argument-2 as arguments, every day at 04:30 AM system time.