CronJob

From Seobility Wiki
Revision as of 14:24, 13 February 2021 by Ralph.ebnet (talk | contribs)

Jump to: navigation, search

Definition

Cronjob
Figure: CronJob - Author: Seobility - License: CC BY-SA 4.0

Cron is a time-based job scheduler in Unix or Unix-like computer operating systems. You can use Cron to schedule jobs, i.e. to execute commands or shell scripts at specified times, dates, or intervals. This allows you, for example, to automate system maintenance or management, to download files from the internet, or to send emails on a regular basis. It is a daemon, i.e. a background process that always runs on the server. The tasks Cron is supposed to perform are called CronJobs. Originally, the name Cron comes from the Greek god of time "chronos".

Application of CronJobs

CronJobs can be used for single commands or for the automated execution of periodically recurring sequential tasks, e.g. for cleaning up databases by removing obsolete entries, log files, and comments, or for creating regular statistics on the number of users of a website.

Other applications include updating RSS feeds, publishing new content to a website on a specific date, generating multiple invoices, or automated newsletter distribution. Likewise, the backup of a database can be scheduled using CronJobs.

Structure and syntax of a CronTab file

Cron uses special configuration files, so-called CronTab files, which contain a list of cron jobs to be executed. CronTab stands for Cron Table. Each line in the CronTab file represents a CronJob. It looks similar to a row of columns separated by a space. Each line specifies when and how often a particular command or script should be executed.

In a CronTab file, empty lines or lines beginning with #, spaces, or tabs are ignored. Lines beginning with # are considered user comments.

Active lines in a CronTab either declare an environment variable or define a CronJob. Comments are not allowed in active lines.

The CronTab file can be accessed in a command-line interpreter by typing "crontab-e". The commands in the CronTab file and their runtime are checked by the Cron daemon, which executes them in the system background. Each CronTab has the following basic structure with six columns arranged like a table:

* * * * * command to execute

The first five columns contain time values which define when a command should be executed. These are, from left to right:

  • Minute, specified as 0 - 59
  • Hour, specified as 0 - 23
  • Day, specified as 1 - 31
  • Month, specified as 1 - 12
  • Weekday, specified as 0 - 7, 0 or 7 are Sundays

For each of these values, the wildcard "*" can be used for execution at any time (i.e. at any minute, hour, day, month or weekday), "*/n" for execution every n (minutes, hours, etc.) and "n,x,y" for execution on / at n, x or y. The last column specifies the relative or absolute path of the script to be executed. The CronJob is performed when the minute, hour, month, and day of the month or weekday match the current time.

Some Cron implementations also support special strings. These strings are used instead of the first five fields in the CronTab file. Each string specifies a specific frequency:

  • @yearly / @annually: Execution once a year at midnight of January 1st (0 0 1 1 *)
  • @monthly: Execution once a month at midnight of the first day of the month (0 0 1 * *)
  • @weekly: execution once a week at midnight on Sunday (0 0 * * 0)
  • @daily: Execution once a day at midnight (0 0 * * *)
  • @hourly: execution at the beginning of each hour (0 * * * * *)
  • @reboot: one-time execution at system start

CronJob examples

An example of a CronJob would be the backup of a database at 2 am each day of the week. In this case, the CronTab file would contain the following:

0 2 * * */bin/sh backup.sh

Sometimes it may be necessary to plan the execution of tasks for selected months only. The following sample script runs in January, May, and August:

* * * 1,5,8 */bin/sh script.sh

If you only want tasks to run on selected days at a specific time, for example, every Sunday and Friday at 5 pm, the CronTab file might look like this:

0 17 * * 0,5/bin/sh script.sh

Importance for online marketing

With CronJobs, you can automate various tasks in online marketing, thus increasing efficiency. In addition to automating time-consuming tasks, CronJobs can be used, for example, to create visitor statistics for a website and automatically send these to a recipient at certain intervals. In conjunction with CMS, CronJobs can be used to automatically calculate metrics or send newsletters on specific dates as part of marketing campaigns.

Related links