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.
Two fundamental components, the principles of which should be understood by any VPS administrator. To put it simply:
Each account has its own crontab table, so all users can set up their own schedule and set some specific tasks.
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:
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.
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:
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.
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:
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.
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
# 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.
There are two files that restrict access to the Task 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.
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:
And this is how you can call the script via wget:
If you have any questions, you can ask our specialists on the contact phone numbers or through Livechat. Thank you for your attention!