CronJob

From Seobility Wiki
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 to, for example, automate system maintenance or management, download files from the internet, or 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 play an important role in automating repetitive tasks online. Some common applications include:

  • Database maintenance: Automating database backup, optimization, and cleanup tasks.
  • System maintenance: Running regular system updates, disk space monitoring, and cleanup processes.
  • Data import/export: Scheduling data synchronization tasks between different systems or services.
  • Notifications: Sending out scheduled alerts, reports, or emails.
  • Website monitoring: Checking website uptime or content changes at regular intervals.
  • Automated reporting: Generating and distributing performance reports for websites or applications.
  • Automating general functions: It’s common for developers to want to automate functions to run at certain times of the day, which is something CronJobs are well suited to.
  • Backup schedules: Creating regular backups of files or directories to prevent data loss.

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

Common issues when using CronJobs

While CronJobs can be very useful, there are also some common pitfalls associated with them, namely:

  • Incorrect cron expression: A common mistake is entering the wrong scheduling syntax, leading to jobs not running at the intended times.
  • Permissions issues: If a CronJob is set up with the wrong user permissions, it may not have the necessary access to execute the required commands.
  • Environment variables: CronJobs may not have access to the same environment variables as the user, which can cause scripts to fail.
  • Overlapping jobs: Without proper management, jobs may overlap, leading to system resource contention.
  • Error handling: It's important to direct the output and errors to log files for debugging. Without proper error handling, it can be difficult to troubleshoot issues.
  • Path issues: CronJobs run in a restricted environment, so full paths to commands and scripts must be used.
  • Email output: By default, Cron sends the output of jobs to the user's email, which can be an issue if not configured correctly or if the volume of output is high.

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.

In summary: CronJob FAQs

What does * mean in Cron?

The asterisk * is used as a wildcard in Cron. * sets the execution of a task to any minute, hour, day, weekday, or month.

What is Cron used for?

Cron allows you to schedule jobs and automate recurring tasks, such as newsletter distribution.

What is a CronTab?

CronTabs are the files that contain the individual CronJobs.

How do you schedule a CronJob that does a backup at 2 am every day?

A scheduled backup that occurs daily at 2 am could look like this:

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

Related links

About the author
Seobility S
The Seobility Wiki team consists of seasoned SEOs, digital marketing professionals, and business experts with combined hands-on experience in SEO, online marketing and web development. All our articles went through a multi-level editorial process to provide you with the best possible quality and truly helpful information. Learn more about the people behind the Seobility Wiki.