19 April 2022 Reading time: 6 minutes

Dmitri Drevko

Web programmer

Cron scheduler: where it can be useful and how to use it in web programming

Ispmanager

What is Cron and how to use it?

 

Cron is a daemon program in the UNIX family of operating systems. It can perform tasks on the server at preset intervals. Thanks to Cron, a web programmer can simplify his work and automate almost any task: from standard backups to more complex ones. I used Cron to check and clean up broken links on a radio station website. I will talk briefly about that experience below, but first, let us look at how to configure Cron manually through the console or in the ispmanager panel if you do not want the hassle of the command line.

Configuring Cron manually in the console

 

The Cron schedule is set by instructions in the crontab files. They specify which commands and at what time the server must execute.

 

Crontab files are created for each user individually in /var/spool/cron/crontabs/ directory. A detailed description of crontab is available in the official documentation, while here I will only concentrate on the main commands.

 

To enter information into crontabs, use the crontab -e command. It opens the contents of the crontab file for editing. Running this command does not interrupt Cron's work - all the processes recorded in the open crontab will run according to the schedule.

 

Basic commands for working with Cron:

  • crontab -l displays the contents of the current schedule file, the active user - the one who is currently logged into the system;
  • crontab -r deletes the current schedule file;
  • crontab – e displays the contents of the current schedule file for editing.

 

To add a new job to crontab, you need to write a line in the crontab that specifies the time of the command and the command itself or links to the script to be executed.

 

The structure of a single line of a crontab file is as follows:

Structure of a single line of a crontab

Each line has six parameters separated by spaces.

In the first five parameters you set the time of the command or script: minute, hour, day, month, day of the week. These values can be entered in the following form:

  • numbers;
  • a comma-separated list of numbers;
  • a range of numbers separated by a hyphen;
  • * or / characters. The * character means that the job is executed each time in the specified period. For example, a job like this: * * * * * * * [command to execute] will be executed every minute. To set the interval, for example, every ten minutes, you need to add a / character to this entry and specify a number: */10 * * * * * [command to execute] - start the job every 10 minutes.

 

The time and date for the execution is taken relative to the time and date set on the server where the Cron job is executed.

 

The sixth parameter specifies the command to execute.

 

There are crontab generator services to create Cron commands manually. They make life easier for novice users:

  • crontab.guru is a good resource for those who are still unfamiliar with the scheduler. Here you can learn command syntax and examples of cron jobs.
  • crontab-generator.orgis a command generator. All you need is to enter the necessary data into the form, and the website will generate a command for the scheduler. You can paste it into a crontab file by opening it with the command crontab -e, or you can save it to a file or even send it by email.

 

If you use ispmanager, there is no need for you to enter commands into crontab - you can specify them through a form directly in the panel. Let me tell you about this way of working with Cron.

 

A few examples of using Cron in ispmanager

Ispmanager is a control panel for web servers and websites. It already includes a tool for working with Cron. To schedule tasks on the server, you do not need to open the terminal - the management is done through forms and buttons in the panel.

 

An example of the default contents of the crontab file for the root user in ispmanager:

[[javascript]] ## ISPsystem acme.sh certs update task 48 3 * * * /usr/local/mgr5/sbin/cron-core sbin/mgrctl -m core [[/javascript]]

The first string shows the task description: ## ISPsystem acme.sh certs update task.

 

The second string states that every day at 3:48 a.m. the command /usr/local/mgr5/sbin/cron-core sbin/mgrctl will be executed with the parameters -m core acmesh.certs.update. The part >/dev/null 2>&1 means that the command report will not be sent to the user's email.

 

The command is executed every 5 minutes:

[[javascript]] ## ISPsystem task which collect system statistics */5 * * * * /usr/local/mgr5/sbin/cron-core sbin/mgrctl -m core sysinfostat >/dev/null 2>&1 [[/javascript]]

 

To avoid having to enter all these commands manually into the crontab file, you can go to the Scheduler section of ispmanager and either edit an existing job or create a new one:

This is what the section with all the created scheduler jobs looks like

How I set up a radio station website with Cron

 

I have used Cron to automate accessibility checks for many Internet resources.

 

Working in a web studio, we were developing a radio station aggregator website. The website was a collection of links to streaming audio from Internet radio stations.

 

From time to time radio stations would stop broadcasting indefinitely. Because of this, there were broken links on the website that did not work. We had to check them around the clock every two or three hours. To do this, the website server sent a request to each link and checked the code that came in response. If it was getting code 200, then everything was fine and the link was working. All other codes meant that the link was not working. Such a radio station needs to be removed from the website and not shown to visitors until the link will return code 200 again.

 

To get rid of the manual checking of about seven hundred links, I used Cron. I wrote a script in PHP that checked the availability of links to audio streams and ran it every two hours. The command was:

[[javascript]] * 2 * * * php check-radios.php >/dev/null 2>&1 [[/javascript]]

 

I added it to Cron through the installed ispmanager. In the type of job execution, I chose the Expert mode - in it, you can set not only a specific time, day, day of the week and month, but also the interval:

The Cron scheduler interface for creating and editing jobs in ispmanager

After that, the created job appears in the Scheduler section with all the set parameters:

A task just created with ispmanager in the scheduler

Using Cron in different CMS

 

Many CMS already have built-in mechanisms to use Cron. They serve to manage website maintenance tasks and usually already work out of the box. They are mainly used to check for updates to different parts of the website management system, be it modules, the core system or search engine indexing.

 

Cron's pitfalls and unpleasant surprises

 

Sometimes Cron can fail and might not work in time.

 

For example, a job is set up on the server, which is scheduled to run once a day to collect data for further processing. The data is very important. However, if something happened on the server at the time when the job should have been executed - the required database did not respond, the server was rebooted or simply did not work because of an ordinary power failure - then Cron is powerless. It will not be able to work and will miss the start of the process. This is worth taking into account: after failures, tasks must be executed manually or wait for the next scheduled start.

Ispmanager for managing websites and web servers

 

Ispmanager helps to create and administer websites: set up the development environment, automate DevOps tasks, make backups, install PHP versions, programming language packages, and much more. Try the free version of ispmanager for 14 days.