Working with the CRON Job Scheduler
Why?
With the CRON scheduler, you can create jobs to execute specific commands or scripts run on a set schedule.
Instructions
In the ispmanager 6 panel
The Job Scheduler in the panel is managed from the "CRON jobs" section.
The "CRON jobs" section has the following options in the main menu:
- Create a job;
- Change job parameters;
- Delete job;
- Enable and disable a job;
- Scheduler Settings;
- Complete the job.
The scheduler settings have two parameters:
- E-mail address – the e-mail address to which the output of the scheduler commands will be sent. In the settings for each job, you can enable the "Do not send report by e-mail" option, if you don’t need a report on its execution;
- Path – the path to the directories of the executable files. A colon is used as a separator.
When you create or modify a job, you can specify the following parameters:
- Command – a command executed by the scheduler;
- Description – a description of the job;
- Schedule – the schedule according to which the job will be automatically executed.
The execution schedule can be customized in basic and expert mode. The expert mode differs from the basic mode only in that it offers more options for customizing the job schedule.
In basic mode, graph setup offers only 3 parameters:
- Frequency;
- One hour to execute the job;
- One minute to execute the job.
In expert mode, the graph can be customized with special characters and numeric values just like through the console:
- "*" represents each new value. For example, every minute, hour, or day;
- "," is used to enumerate the values without spaces;
- "*/" represents each value in a specific increment;
- "-" represents a range of values.
For example, to perform the job on the 1st and 30th days of each month from 10 am to 10 pm and every 5 minutes, set the parameters as shown in the screenshot below.
Example
As an example, let's create a job to write the system time to a file every 2 minutes.
Under "File Manager", create the file servtimetest
, for this example, in the "root" directory. Enter the following lines in the file:
#! /bin/sh
date>> date-out.txt
Save the changes and copy the path to the file from the "File name" field. This will be needed when specifying the path of the executable file in the job.
After creating and filling out the script, it needs to be assigned the correct permissions to be executable. In the "File Manager" section, select the file, click the "Attributes" button and specify the permissions as in the screenshot below.
After creating the script, go to the "CRON jobs" section to create the job. Click the "Create plan" button and in the new tab and in the "Command" field, specify the path to the script that was copied earlier.
Switch to expert schedule mode and specify "*/2" in the "Minutes" field and "*" in all others.
Once the job is saved, every two minutes, the server time will be recorded in the date-out.txt
file that will appear next to the script.
Via terminal
Through the console, you can work with the CRON job scheduler using the crontab
command:
- To see a list of existing user jobs, use the
crontab -l
command; - To enter edit mode, use the command
crontab -e
.
The list of jobs in the console is displayed as follows:
root@example:~# crontab -l
## Ispmanager update package cache task
26 1 * * * /usr/local/mgr5/sbin/cron-ispmgr sbin/pkgupdate.sh >/dev/null 2>&1
## Ispmanager stat handler (ispmgr)
37 22 * * * /usr/local/mgr5/sbin/cron-ispmgr sbin/mgrctl -m ispmgr ispstat >/dev/null 2>&1
## Ispmanager periodic tasks handler (ispmgr)
* * * * * /usr/local/mgr5/sbin/cron-ispmgr sbin/mgrctl -m ispmgr periodic >/dev/null 2>&1
Each new job usually starts with a schedule in this order: minutes, hours, days of the month, months, and days of the week. After the schedule, the path to the executable file is specified.
In the line above the job itself, you can set a description for it, which is indicated as a comment (a line with "##" symbols).
Example
As an example, let's create a job to write the system time to a file every 2 minutes.
Create a file and add the following lines to it using a text editor such as "nano":
nano /root/servtimetest
#! /bin/sh
date>> date-out.txt
Specify the correct permissions for the file:
сhmod 775 /root/servtimetest
Go to the CRON Job Edit mode and create a new job:
crontab -e
## save the system_date
*/2 * * * * /root/servtimetest >/dev/null 2>&1
Once the job is saved, every two minutes, the server time will be recorded in the date-out.txt
file that will appear next to the script.
You can view the job’s progress in the system log:
/var/log/syslog
- for Debian systems;/var/log/messages
- for RHEL systems.
Exporting and importing CRON jobs
To export or import CRON jobs you have to connect to the server via SSH.
To export list of CRON jobs to the file with custom name execute following command:
crontab -l > <filename>
Example of exporting the list to the file /root/exported-cron
:
crontab -l > /root/exported-cron
PLEASE NOTE!
Importing the list of the CRON jobs will replace current CRON jobs.'
To import list of CRON jobs from the file execute following command:
crontab <filename>
Example of importing the list from the file /root/exported-cron
:
crontab /root/exported-cron
If it is required to add the list of CRON jobs from the file, you can merge lists before importing. To do this, execute following command:
- Export list of current CRON jobs to the file:
crontab -l > /root/current-cron
- Merge list of current jobs and list of exported jobs using "cat":
cat current-cron exported-cron > merged-cron
- Import list of CRON jobs from merged file:
crontab merged-cron