CronJob – automatically execute essential tasks

The first answer to the question “What is a CronJob?” is: a “daemon”. The second answer is: a god. Confused? The answers refer only to the etymology of the name. Strictly speaking, Cron does stand for “command run on notice”. However, the abbreviation also alludes to Chronos, the ancient Greek god of time. In operating systems such as Linux or macOS, however, the command runs as a cron daemon (also called a cron system). So, there you have it.

The allusion to the Greek god of time is, by the way, an excellent mnemonic to recall the purpose of a CronJob. The cron system runs in the background of an operating system and can automatically execute tasks (jobs) at specified times. So, if you put cron system and another name for a fixed task together, it results in the term CronJob.

What is a CronJob?

A CronJob is a task that runs automatically in operating systems. The system regularly issues commands for these jobs. This can be a single task, but usually there are several complex tasks, i.e., several CronJobs. The advantage is that CronJobs can be highly individualized.

Each CronJob consists of three components:

  • A Script that is to be executed
  • A Command that will execute the script on a regular basis
  • An Action or output of the script

Practical examples for its use are recurring server tasks. Among other things, newsletter distribution, statistical surveys or database cleaning are automatically initiated by CronJobs.

All operating systems that have a cron system or a cron daemon can use CronJobs. This includes all Unix and BSD (Berkeley Software Distribution) systems such as macOS or Linux.

The operating systems can have CronJobs run according to their motto “Command run on notice” at pre-set intervals. Routine tasks can therefore run completely independently from extra user input. In addition to an operating system with a cron daemon, the following prerequisites must be present for CronJobs to be set up:

Note

Special user interfaces exist for the easy setup of CronJobs as well as web hosting packages that provide pre-configured CronJobs for web projects. For web hosting with CronJobs, we recommend looking at our Cronjob Help article.

What kind of tasks can CronJobs do?

Probably the most common application of CronJobs is data backup. For example, if you want to create a backup with tar at regular intervals to archive data, CronJobs are ideal. Cleaning up databases and processing visitor statistics on websites are also among the typical uses of CronJobs.

CronJobs can interact with a content managements system to automatically send newsletters or check stock levels of online stores. You monitor server load and can automatically adjust new information in the product description, for example, the average user rating.

Note

The automated tasks of CronJobs are so diverse that they must be used with care because they require many resources. Therefore, it is important to consider whether it really makes sense to employ a CronJob, and when to set it up. Suitable PHP scripts ensure that CronJobs only run when they do not interfere with other processes.

The most important tasks briefly

  • Clean databases: Obsolete records are removed; this includes outdated comments or login entries that are no longer up to date.
  • Create statistics: CronJobs can, for example, update the page views of a site daily.
  • RSS feed update: The XML file can be regenerated regularly via CronJob.
  • Planning and publishing of new content
  • Newsletters: Emails can be sent over a period of time.
  • Generate invoices: Invoices can be generated and sent independently ­– bundled and at fixed time intervals.
  • Database backup: This mainly refers to smaller databases that are less than 20 MB in size.
  • Machine learning: Via CronJob, artificial intelligences can be supplied with new data records at a fixed time.
  • Updates: Software updates can be provided at a specified time.

What does a normal CronJob command look like?

The possibility of individualization is one great advantage of a CronJob. To customize CronJobs, there is a basic set of three variables that can be executed at any time:

  1. PATH: shows the path where commands or a script is searched for.
  2. SHELL: directs the command interpreter to execute a script.
  3. MAILTO: shows the users with whom the results of the CronJob should be shared; by default, this is the subscriber.

Users can define CronJobs themselves with these three variables and any number of other variables. Usually SHELL, PHP and PERL scripts are used for the commands.

Note

You can get more information about the commands in Linux and Co. in our basic article about Linux commands.

How does a CronJob work?

For a CronJob to do its work, a command must be issued in advance. The cron daemon then works in the background and performs non-interactive jobs. Incidentally, this function is known as “services” under Windows.

CronJobs are basically idle and waiting for their command, either directly within the operating system or from another computer on the network. The command is given via a cron file (simple text file), which specifies the job at a specific time.

CronJobs are defined and configured in a table, also known as a crontab. It lists the time and the command sequence to be executed.

Note

The program used to edit individual crontabs is also named “Crontab”.

The default system crontab file “crontab” can be found in the crontab directory /etc/cron.*/.

Web hosting with a personal consultant

Fast and scalable, including a free domain for the first year and email address, trust web hosting from IONOS!

Domain
Wildcard SSL
24/7 support

Four short CronJob examples

Crontabs can be edited with the crontab program. The command required for this is:

crontab -e

The syntax is quite simple, all things considered. Each cronjob has the following format:

*     *     *     *     * Command to be executed
-      -      -      -     -
|     |     |     |     |
|     |     |     |     +----- Weekday (0 - 7) (Sunday is 0 and 7)
|     |     |     +------- Month(1 - 12)
|     |     +--------- Day (1 - 31)
|     +----------- Hour (0 - 23)
+------------- Minute (0 - 59)

The asterisk * means that a CronJob should always take place on the respective day, month, hour or minute. For the weekdays and months, the first three letters can be used, like ‘sun’ for Sunday. Information about exact time periods cannot be given with this form of notation.

For more precise information about the time periods, the asterisk is replaced by digits. If you set the first digit to 8-10, the command will be executed in the 8th, 9th, and 10th minute. If you set */2, the command will be executed every second minute. Both can be combined. In total, three time values are possible for the first five digits:

  • * = execution always (at each specification)
  • */n = execution every n (time interval)
  • n,x,y = execution at/on n,x,y

Example 1: CronJob backups

To run a backup every morning at 6am, you need the following command:

0 6 * * * /usr/bin/backup.sh

Example 2: Sound the alarm

CronJobs can also be used to play a sound at regular intervals:

*/30 * * * * /usr/bin/play_sound.sh

Example 3: Reminders

Reminder emails at 8am and 6pm are sent by the following cronjob:

0 8,18 * * * /usr/bin/send_reminder_mail.sh

Example 4: Thanks, Admins!

Or you can use CronJobs to never forget the system administrator’s day again:

0 0 30 07 * (echo “Message body”|mail -s “Message Subject.” user@domain.xy)

Wait! We’ve got something for you!
Have a look at our great prices for different domain extensions.


Enter the web address of your choice in the search bar to check its availability.
.org
$1/1st year
then $20/year
.com
$1/1st year
then $15/year
.info
$1/1st year
then $20/year
.me
$1/1st year
then $20/year