A detailed list of cron services in a Linux system

Source: Internet
Author: User

To use the Linux cron service, you must install the Vixie-cron RPM package and must be running the Crond service. To determine if the package is installed, use the Rpm-q vixie-cron command. To determine whether the service is running, use the/sbin/service crond status command.
Crond is a command that Linux uses to execute programs on a regular basis. When the operating system is installed, the task Scheduling command is started by default. The Crond order will periodically check whether there is any work to be done and will automatically carry out the work if there is any work to be done. and Linux task scheduling is mainly divided into the following two categories:
1, the system implementation of the work: the system periodically to perform the work, such as backup system data, clean cache
2, Personal work: a user to do the work on a regular basis, such as every 10 minutes to check the mail server for new letters, these work can be set by each user

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

The code is as follows Copy Code
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
# Run-parts
* * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly

The first four lines are variables used to configure the Linux cron task's operating environment.
The value of the shell variable tells the system which shell environment to use (in this case, the bash shell);
The path variable defines the paths used to execute the command.
The output of the MAILTO cron task is mailed to the user name defined by the MAILTO variable. If the MAILTO variable is defined as a blank string (mailto= ""), the e-mail message is not sent.
The home variable can be used to set the master directory to use when executing commands or scripts.

/etc/cron.allow and/etc/cron.deny files are used to restrict the use of cron. Both of these use control files are formatted as one user per line. No spaces are allowed for two files. If the control file is modified, the cron daemon (crond) does not have to be restarted. Using control files is read every time a user adds or deletes a cron task. Regardless of how the rules are used in the control file, the root user can always use cron.

If the Cron.allow file exists, only the users listed therein 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.

In addition, there are two directory Cron,anacron under/var/spool, where the cron directory has the contents of each user's own crontab added through the crontab-e. The Anacron directory below is a record of the last execution time of cron.daily,cron.monthly,cron.weekly. The Linux cron service is to read every minute of the/var/spool/cron,/etc/crontab,/etc/cron.d underneath all the content.

Linux cron basic format:
* * * * command
Time-sharing and Lunar Week command
The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 for 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
The 5th list of the week 0~6 (0 for Sunday)
6th column the command to run

Some examples of crontab files:
The following example shows 21:30 restart Apache per night.

The code is as follows Copy Code

* * * */usr/local/etc/rc.d/lighttpd restart

The following example shows a 4:45 reboot of Apache for 1, 10, and 22nd per month.

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart

The following example shows the 1:10 reboot of Apache every Saturday and Sunday.

1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart

The following example shows the restart of Apache every 30 minutes from 18:00 to 23:00 every day.

0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart

The following example indicates that 11:00 PM restarts Apache every Saturday.

0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart

Restart Apache every hour

* */1 * * * */usr/local/etc/rc.d/lighttpd restart

From 11 o'clock to 7 in the morning, restart Apache every hour.

* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart

4th per month with 11 points per Monday to Wednesday restart Apache

0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart

Restart Apache at 4 o ' January 1.

0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart

In December, every day at 6 to 12, every 3 hours of the morning/usr/bin/backup are performed:

0 6-12/3 */usr/bin/backup

Send a letter to Alex@domain.name from Monday to Friday every 5:00:

0 * * 1-5 mail-s "HI" Alex@domain.name </tmp/maildata

Every month at midnight 0:20, 2:20, 4:20 .... Execute echo "haha"

0-23/2 * * * echo "haha"

Attention:
When the program is executed at the time you specify, the system will send you a letter showing the execution of the program, if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line blank

  code is as follows copy code

#每天早上6点10分
&NBSP
6 * * * Date

#每两个小时
 
0 */2 * * * Date
 www.111cn.net

#晚上11点到早上8点之间每两个 Hours, 8 in the morning.
 0 23-7/2,8 * * Date


#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
 
0 4 * mon-wed Date
&NBSP
#1月份日早上4点
 
0 4 1 * Date

Linux cron Command Summary:
crontab file [-u user]-replaces the current crontab with the specified files.
Crontab-[-u user]-replaces the current crontab with standard input.
crontab-1[user]-lists the user's current crontab.
crontab-e[user]-Edit user's current crontab.
crontab-d[user]-deletes the user's current crontab.
Crontab-c dir-Specifies the directory for crontab.

crontab file format: M H d M D cmd.
M: Minutes (0-59).
H: Hours (0-23).
D: Days (1-31).
M: Month (1-12).
D: Days of the week (0~6,0 for Sunday).
CMD to run the program, the program was sent to SH execution, this shell only user,home,shell these three environment variables

When F1 is *, it means that the PROGRAM,F2 is executed every minute for *, and the rest of the program is executed every hour.
When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to be performed from A to a B-hour, and the remainder of the analogy
When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest
When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, and the rest of the analogy
The user can also store all the settings in the file file, using crontab file to set the schedule.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.