What is CRON and how to use it

What is CRON and how to use it

11:31, 01.02.2022

Contenido del artículo
arrow

  • Cron and crontab: what is it?
  • How to use Cron
  • How Cron and crontab work via the control panel
  • How Cron and crontab work via the SSH protocol
  • Initial Cron setup
  • Setting restrictions in Cron
  • Additional information

Cron is one of the most popular daemons on Linux, which is essentially an advanced task scheduler. It is an important component of all Linux-like systems and is used by administrators to schedule certain programs and scripts to run at specified times. The administrator can set the periodicity of launching scripts, adjust the duration of their operation, etc. In this article, we will try to explain as much as possible how to use cron via control panel and SSH protocol, share tips on the initial configuration of the program, and give a small tour on cron syntax.

Cron and crontab: what is it?

Two fundamental components, the principles of which should be understood by any VPS administrator. To put it simply:

  1. Cron is a task scheduler. You can use it to configure certain scripts to run at specified intervals. For example, every day at 8 PM, you can collect static data from the server, generate logs, do backups, or set up checks for system updates. All in all, a useful thing, no matter how you look at it.
  2. Crontab is a table where the schedule of launching scripts and software is recorded. It has a special format that is supported by the daemon and read by the server.

Each account has its own crontab table, so all users can set up their own schedule and set some specific tasks.

How to use Cron

This daemon is widely used to configure repetitive tasks when the server is running. In fact, the list of possible usage scenarios is limited only by the skills and imagination of the administrator. Here are a few examples of scenarios for using Cron:

  • scheduling alerts that provide information about server status, physical hardware metrics, etc.;
  • generating security logs;
  • correcting system time for hardware and software;
  • data backup;

The rich syntax of the daemon allows setting any frequency of tasks, starting from 1 hour and up to once a year. The ability to use Cron intelligently is important because it reduces the need for intervention in the server by the administrator. In fact, the work of VPS can be fully automated, and when under the control of a specialist there are several dozen servers at once, the ability to properly configure the task scheduler is a vital thing.

How Cron and crontab work via the control panel

Many hosting providers have their own control panel, where the developers have made it possible to set up the task scheduler in a more convenient way through a graphical interface. For example, ISPmanager has such a scheduler, and in order to form tasks it is enough to go to the "CRON Scheduler" section, located in the left menu, and click on "Create".

In the window that appears, you will need to enter:

  1. Email Address. Here you enter the email address you want to receive notifications of task status. You can leave it blank, if you do not need it.
  2. Command - here we enter the path to the program that we put to execution. If you are planning to run a php-script, you must also write the path to the interpreter. For example: /usr/bin/php7.0 /var/www/new_user/data/www/mysite/script.php.
  3. Schedule - configurable in basic or expert mode, here you set the frequency of tasks.
  4. Description - just a comment to make it easier to navigate in the scheduler. If you have a lot of tasks, it's a very useful thing, don't neglect it.

As you can see, there is nothing complicated, and setting up cron tasks through the ISPmanager control panel is very easy. The same can be said for most other control panels such as cPanel, VirtualMin, DirectAdmin, etc.

what is cron and how to use it

How Cron and crontab work via the SSH protocol

There are times when setting up the scheduler via the control panel is impossible, or inconvenient, or just not interesting. But an experienced administrator can easily configure everything the old-fashioned way, through the command line. To do that we first need to connect to the server using the SSH protocol.

Let's look at 5 key parameters of the Crontab commands:

  • crontab -e - to edit or create schedule files for the current account;
  • crontab -l - displays the contents of the schedule table in the current account;
  • crontab -r - clears the schedule file;
  • crontab -r - show date when config was last opened;
  • crontab -u user - work with schedules of individual users.

The last option is available only for accounts with superuser privileges.

Well, to work with the Cron scheduler through the console, you should first perform the initial configuration of the program and remove any possible restrictions.

Initial Cron setup

We have already written above that each user has his own individual schedule table, but by default it does not exist and you need to create it. The command crontab -e allows you to generate a table which will appear in /var/spool/cron directory. The file will be totally empty, so you have to fill in the parameters manually by applying your knowledge of cron syntax. Let's take a short look at the syntax of the daemon:

# crontab -e
SHELL=/bin/bash
MAILTO=yourmail@domain.com
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
 
# Details are shown below
 
# An example of setting a task through the scheduler:
# .---------------- minutes (0 - 59)
# |  .------------- hours (0 - 23)
# |  |  .---------- days (1 - 31)
# |  |  |  .------- months (1 - 12) OR may,jun,sept,aug ...
# |  |  |  |  .---- weekdays (0 - 6) OR mon,tue,wed,thu,fri,sat,sun
# |  |  |  |  |
# *  *  *  *  * the user name AND the command you want to schedule
 
# creating a backup of the entire operating system using a custom script
01 01 * * * /usr/local/bin/bckp -vbd1 ; /usr/local/bin/bckp -vbd2
 
# to set the time correspondence between the OS and the hardware, the following syntax is used:
03 05 * * * /sbin/hwclock -systohc
 
# and this is how you can schedule OS updates:
25 04 1 * * /usr/bin/apt-get update

For the initial setup, the first three lines are the most important. Here we specify the shell for the Cron, the e-mail address to receive notifications, and the path to the environment. The syntax of the daemon includes the format of time and command which should be executed by the scheduler at a certain moment of time.

Here are a few examples of using scripts with the scheduler for clarity. This is how you can set a script to run every Tuesday at 10:00 a.m. and at 7:00 p.m:

2 10,19 * * 1 /home/john/script.sh

And this is how the daily script is set up at 0:30 and 12:30:

30 */12 * * * script.sh

In general, there is nothing complicated here, everything is done elementary. If necessary, you can even schedule several tasks in a single entry by separating them with semicolons:

* * * * * /scripts/script.sh; /scripts/scrit2.sh

Finally, here is an example of a script which will run the search for system and software updates through the apt package manager every month, on the first day at 4am:

00 04 1 * * /usr/bin/apt-get update

If you want to learn the intricacies of the daemon in more depth, we recommend studying the documentation on the official Linux administration encyclopedias.

Setting restrictions in Cron

There are two files that restrict access to the Task Scheduler:

  • /etc/cron.allow - this contains data about user accounts that can schedule tasks;
  • /etc/cron.deny - and here lists those users who are not allowed to use the scheduler.

The entries in both files should be kept as follows: one user name on each line without spaces. They are so-called access control files, and they will be read every time a user tries to work with the scheduler. If the account name is listed in both files at the same time, the /etc/cron.deny file will be ignored. If both of these files do not exist, however, there will be no scheduler-related restrictions on the server.

Additional information

First, don't be indifferent to specifying the mail address in the scheduler parameters because this will allow you to receive important messages about events on the server in time. If there are any errors, you will be able to fix them promptly.

Secondly, many people don't know how to add a PHP script to the scheduler. Let's look at an example of a script with this path: /var/www/user/data/www/vashdomain.com/script.php. This is how you can pass it to the interpreter:

/usr/bin/php /var/www/user/data/www/yourdomain.com/script.php

And this is how you can call the script via wget:

/usr/bin/wget yourdomain.com/script.php

If you have any questions, you can ask our specialists on the contact phone numbers or through Livechat. Thank you for your attention!

views 7m, 16s
views 2
Compartir

¿Te ha resultado útil este artículo?

Ofertas populares de VPS

Otros artículos sobre este tema

cookie

¿Acepta las cookies y la política de privacidad?

Utilizamos cookies para asegurar que damos la mejor experiencia en nuestro sitio web. Si continúa sin cambiar la configuración, asumiremos que acepta recibir todas las cookies del sitio web HostZealot.