crontab installation and usage (timed task)

The crontab command is common in Unix and Linux operating systems , and is used to set instructions to be executed periodically. This command reads instructions from the standard input device and places them in a "crontab" file for later reading and execution. Usually, the instructions stored by crontab are activated by the daemon. crond often runs in the background, checking every minute if there are scheduled jobs to execute. Such jobs are generally referred to as cron jobs.

 

1. Installation

[root@CentOS ~]# yum -y install vixie-cron
[root@CentOS ~]# yum -y install crontabs

Description:
The vixie-cron package is the main cron program; the
crontabs package is the program used to install, uninstall, or list the tables used to drive the cron daemon.

Two, placement

Cron is a built-in service of linux, but it does not start automatically. You can use the following methods to start and close the service:
service crond start //Start the service
service crond stop //Close the service
service crond restart //Restart the service
service crond reload // Reload configuration
service crond status //View crontab service status

Add to the CentOS system to automatically start at boot: chkconfig --level 345 crond on

The main cron configuration file is  /etc/crontab , which includes the following lines:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * The first four lines of root run-parts /etc/cron.monthly

are variables used to configure the running environment of cron tasks.
The value of the SHELL variable tells the system which shell environment to use (the bash shell in this example); the
PATH variable defines the path used to execute the command.
The output of the cron job is mailed to the username defined by the MAILTO variable.
If the MAILTO variable is defined as a blank string (MAILTO=""), the email will not be sent.
The HOME variable can be used to set the home directory to use when executing commands or scripts.

 

Restricting the use of cron: The

/etc/cron.allow
and /etc/cron.deny  files are used to restrict the use of cron.
Both formats of the usage control file are one user per line.
Both files do not allow spaces.
If the usage control file is modified, the cron daemon (crond) does not have to be restarted.
The usage control file is read every time the user adds or removes a cron job.

cron is always available to root, regardless of what is specified in the usage control file.

If the cron.allow file exists, only the users listed in it are allowed to use cron, and the cron.deny file is ignored.
If the cron.allow file does not exist, all users listed in cron.deny are prohibited from using cron.

 

Three, crontab command

Function : Set the timer.

Syntax : crontab [-u <user name>][configuration file] or crontab [-u <user name>][-elr]
Explanation : cron is a resident service that provides timer functions, allowing users to time to execute preset instructions or programs. As long as the user can edit the configuration file of the timer, the function of the timer can be used. The configuration file format is as follows: Minute Hour Day Month DayOFWeek Command

Parameters:
-e Edit the timer settings for this user.
-l List timer settings for this user.
-r removes timer settings for this user.
-u <username> Specifies the username to set the timer for.

 

Format:
* * * * * command
Time division, day, month and week command

The first column indicates minutes 1 to 59 every minute is indicated by * or */1
The second column indicates hours 1 to 23 (0 indicates 0 o'clock)
The third column indicates the date 1 to 31
, the fourth column represents the month, 1 to 12
, the fifth column, the identification number, the day of the week, 0 to 6 (0 means Sunday),
and the sixth column of the command to run

 

Example:
*/5 * * * * root ab -n 2000 http://60.217.229.252/250k.jpg

The above example means that the simulated user visits http://60.217.229.252/250k.jpg 2000 times every 5 minutes

 

30 21 * * * /usr/local/etc/rc.d/lighttpd restart
The above example means restart apache at 21:30 every night.

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
The above example means restart apache at 4:45 on the 1st, 10th, and 22nd of every month.

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every Saturday and Sunday at 1:10.

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every 30 minutes between 18:00 and 23:00 every day.

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every Saturday at 11:00 pm.

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
restarts apache every hour

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
11pm Restart apache every hour between 7am and

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
4th of every month and 11am every Monday to Wednesday

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
Restart apache at 4:00 on January 1st

*/30 * * * * /usr/sbin/ntpdate 210.72.145.44
Synchronize the time every half hour

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325302145&siteId=291194637