CronJob


CronJobs are recurring tasks that can be executed automatically on Unix-like operating systems like Linux or iOS/Mac OS X as well as on server environments. The so-called Cron daemon is the basis for CronJobs. It acts as a temporal impulse for the different tasks and runs in the background of the system as soon as it is started. If CronJobs are defined, they are automatically executed by the system at the specified times. Frequent CronJobs are file management, deleting or archiving of data or the creation of visitor statistics of the server or website. The acronym Cron stands for "command run on notice".


History

Originally, Cron was a system service in Unix that was used by several users of the system. The CronTab was started every minute; commands noted there were executed by the system with a timestamp and the user’s rights (root server). It was used to manage the system. This function has been preserved until today - however, today's Cron daemons no longer use virtual time (e.g. when the user calls Cron), but real time for constantly running servers.

Similar to batch processing or batch programming, CronJobs are processed successively and sequentially. The data is noted in a table with certain arguments, which contain information about the time and commands of the execution. The advantage of Cron is the exact specification of the timestamp that triggers a CronJob. While batch programming is often used in the installation and configuration of servers, CronJobs are often used in the system’s administration. This is mostly done at times when there are only a few visitors to the website, as CronJobs can take up considerable resources of the server. Various analysis and administration tasks can be handled precisely and automatically with Cron.


Functionality

The requirements for using CronJobs are a Unix-like server such as Linux or BSD operating systems (BSD: Berkely Software Distribution) and a dedicated server that is not shared with other customers. Although it is possible to run CronJobs on shared servers, it would also use up the resources of the other customers. The web server is accessed via SSH (Secure Shell) or Telnet client to establish a secure connection. [1] Uploading Cron files and scripts can be done using an FTP program.


The File crontab

Basis for different CronJobs is the file crontab. It can be opened with the command

 crontab -e

in any command line editor. Programs like GNOME Schedule offer a graphical user interface for managing CronJobs. [2] In addition, many websites like corntab.com or cronjob.de offer visual user interfaces and other services.

The CronTab consists of six columns, which are arranged in tabular form. The first five columns contain the timestamp in the form of minutes, hours, days, month, weekday. The last column contains the command to be executed. Each column is separated by a space; several entries can be separated by commas.

The syntax is as follows:


 minute hour day month day-of-week command-line-to-execute

Possible values for the first five columns are

  • 0 - 59 for minute
  • 0 - 23 for hour
  • 1 - 31 for day
  • 1 - 12 for month
  • 0 - 7 for day of week

The character "*" is also often used. It is synonymous to all time units of the respective column. For example, for each day or month. With @reeboot, @yearly, @monthly, @weekly, @daily and @hourly CronJobs can be executed regularly if no exact timestamp should be assigned. In the sixth column the absolute or relative path of the script to be executed is usually noted.


Cron Variables

The commands to be executed are integrated into the crontab in the form of environment / context variables. PHP, PERL or SHELL scripts are mostly used. A selection of possible commands:

  • PATH: Path specifies the path where Cron searches for scripts or commands.
  • SHELL: Shell specifies the command line interpreter that should execute the script. The default setting is "/bin/sh". There are different interpreters available under Unix (For example /bin/tcsh or /bin/csh).
  • MAILTO: With mailto you can specify users to whom Cron sends the results of the script after it has been executed. The default setting is the user who requests the script.

For each CronJob different variables can be defined, so that the timestamps, commands and the output of the script can be adjusted to the respective user. Furthermore, it is possible to document the work of the script via LOGFILE.


Example for a CronJob

A script to be executed daily at 5:30 am. The output is sent to the email address.

 MAILTO=beispiel.empfänger@mail.de
30 5 * * * /usr/local/bin/pfad/test.php


Significance for search engine optimization

CronJobs are an effective way to automate time-consuming administration and evaluation tasks on a server. [3] Databases can be cleaned and saved as backup. Files are archived or deleted. The system is maintained automatically. In addition, visitor statistics can be created for a web server, which are sent to a user on a daily basis, for example. Thus CronJobs are a facilitation for a web analysis based on log files. The data that a server stores is valuable information for optimizing a website.

Since CronJobs work together with scripts and partly also with content management systems, the application possibilities are relatively large. Depending on the script and the content environment, newsletters can be sent, stock levels can be checked or metrics such as top sellers and article ratings can be recalculated regularly. In addition, the server load can be controlled to a certain extent with CronJobs. CronJobs are therefore particularly useful at times when there are only a few visitors to the website. Access by Cron requires resources. It should not be at the expense of the server and should not increase the loading times of normal visitors when calling the website. This aspect often goes unnoticed.


References

  1. How to Create a Cron Job (Scheduled Task) for Your Website or Blog thesitewizard.com. Opened on 16.11.2020
  2. Cron wiki.ubuntuusers.de. Opened on 16.11.2020
  3. Was Cronjob sind und wofür sie gut sind cronjob-tipps.de. Opened on 16.11.2020