Linux crontab command details (including configuration file path) __linux

Source: Internet
Author: User
Link: http://blog.chinaunix.net/uid-20448327-id-4195269.html

In Linux, a task can be configured to run automatically for a specified period of time, a specified date, or if the system average load is below a specified number. Red Hat Enterprise Linux is preconfigured to run critical system tasks so that the system can be updated at all times. For example, the Slocate database used by the Locate command is updated daily. System administrators can use automated tasks to perform regular backups, monitor systems, run custom scripts, and so on.

Red Hat Enterprise Linux has several tools for automating tasks: cron, at, and batch.

The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. 1.cron

Cron is a daemon that can be used to schedule execution of repetitive tasks based on a combination of time, date, month, and week.

Cron assumes that the system is running continuously. If the system is not running when a task is scheduled, the task is not executed.

To use the 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. 1.1. Configure Cron Tasks

Cron's main 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 0 1 * * * * 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
Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name.

The first four lines are variables used to configure the Cron task's running 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 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.

Each row in the/etc/crontab file represents a task, which is in the format: Minute hour day month DayOfWeek command

minute-minutes, any integers from 0 to 59

hour-hours, any integer from 0 to 23

day-date, any integer from 1 to 31 (if a month is specified, it must be a valid date for that month)

Month-month, any integer from 1 to 12 (or the English abbreviation for the month used, such as the long, Feb, etc.)

Dayofweek-week, any integer from 0 to 7, where 0 or 7 represent Sunday (or use the English shorthand for the week, such as sun, Mon, etc.)

command-commands to execute (commands can be commands such as Ls/proc >>/tmp/proc, or commands that execute scripts you write yourself.) )

In any of these values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that the command is executed every month after other constraints are met.

A dash (-) between integers specifies a range of integers. For example, 1-4 means integers 1, 2, 3, and 4.

Specify a list with a series of values separated by commas (,). For example, 3, 4, 6, 8 indicate these four specified integers.

The forward slash (/) can be used to specify the interval frequency. Adding/<integer> after a range means that you can skip integers within the range. For example, 0-59/2 can be used to define every two minutes in the minute field. The interval frequency value can also be used with the asterisk. For example, a */3 value can be used in the month field to indicate that a task is run every three months.

The line that starts with the pound sign (#) is a comment and is not processed.

As you can see in the/etc/crontab file, it uses Run-parts scripts to perform/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly, and/etc/cron.monthly Scripts in the directory, which are executed hourly, daily, weekly, or monthly. The files in these directories should be shell scripts.

If a  cron  task needs to be performed according to schedule, not hourly, daily, weekly, or monthly, it can be added to the  /etc/cron.d  directory. All files in the directory use the same syntax as in  /etc/crontab . # record the memory usage of the system every monday  #  at 3:30am in the file /tmp/meminfo  30 3 * * mon cat  /proc/meminfo >> /tmp/meminfo  # run custom script the  first day of every month at 4:10am  10 4 1 * * / root/scripts/backup.sh

users other than the root user can use the Crontab tool to configure cron tasks. All user-defined crontab are saved in the/var/spool/cron directory and executed using the identity of the user who created them. To create a crontab project as a user, log on as the user, and then type the CRONTAB-E command to edit the crontab of the user by using the editor specified by the VISUAL or EDITOR environment variable. The file uses the same format and/etc/crontab. When changes to Crontab are saved, the crontab file is saved according to the user name and written to the file/var/spool/cron/username.

the cron daemon checks for changes in/etc/crontab files, etc/cron.d/directories, and/var/spool/cron directories every minute. If a change is found, they are loaded into memory. This way, when a crontab file changes, you do not have to restart the daemon.


1.2. Control the use of cron

/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.

The root user can always use cron, regardless of the rules in the control file.

if cron.allow is an empty file, it indicates that no user can schedule a job. If the file does not exist and there is another file/usr/lib/cron/cron.deny, only users who are not included in this file can use the crontab command. If it is an empty file, it indicates that any user can schedule a job. Two files exist at the same time Cron.allow first, if none exists, only super users can schedule jobs.


1.3. Start and stop services

To start the Cron service, use the/sbin/service crond Start command. To stop the service, use the/sbin/service crond Stop command. It is recommended that you start the service at boot time.


1.4 Linux--crontab Use detailed

Name: crontab
Use Rights: All users
How to use:
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.
CMD to run the program, the program was sent to SH execution, this shell only user,home,shell these three environment variables
Description
Crontab is used to allow the user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user is the time table that specifies the user, as long as you have permission (for example, root) to specify the schedule for others. If you do not use-u user, it means setting your own schedule.

Parameters:
CRONTAB-E: Execute the text editor to set the time table, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variables to specify the use of the text editor (for example, Setenv VISUAL Joe)
Crontab-r: Deletes the current schedule table
Crontab-l: List the current schedule
crontab file [-u user]-replaces the current crontab with the specified files.
The user can also store all the settings in the file file, using crontab file to set the schedule.

Example:
#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS


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


* * * */usr/local/etc/rc.d/lighttpd restart
The above example represents 21:30 restart Apache per night.

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 reboot of Apache for 1, 10, and 22nd per month.

1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the 1:10 reboot of Apache every Saturday and Sunday.

0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that Apache is restarted every 30 minutes from 18:00 to 23:00 every day.

0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that 11:00 PM restarts Apache every Saturday.

* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart Apache every hour

* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart Apache every hour.

0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month with 11 points per Monday to Wednesday restart Apache

0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
Restart Apache at 4 o ' January 1.

Separated by commas in the middle of each two time value.
In addition to the numbers there are a few special symbols is "*", "/" and "-", ",", * represents all the values in the range of numbers, "/" for each meaning, "*/5″ said every 5 units,"-"representing from a number to a number,", "separate several discrete numbers.


Every morning at 6.
0 6 * * * echo "Good morning." >>/tmp/test.txt//Note that pure echo does not see any output from the screen because cron emails any output to Root's mailbox.

Every two hours
0 */2 * * echo "Have a break now." >>/tmp/test.txt

Between 11 o'clock and 8 in the morning, every two hours, eight in the morning.
0 23-7/2,8 * * * echo "Have a Good Dream:)" >>/tmp/test.txt

after each edit of a user's cron settings, Cron automatically generates a file with the same name as this user under/var/spool/cron, and the user's cron information is recorded in this file, which is not directly editable and can only be edited with CRONTAB-E. After Cron starts, read the file every time it is written, and check if you want to execute the commands inside. Therefore, you do not need to restart the Cron service after this file has been modified.

[For example] in every Monday, three, five 3:00 system into the maintenance state, reboot the system. The following fields should be written in the crontab file:
* * 1,3,5 shutdown-r +5

Then save the file as Foxy.cron, and then type crontab foxy.cron to install the file.

[Example] 10 minutes per hour, 40 minutes to execute the Innd/bbslin in the user directory:
10,40 * * * * Innd/bbslink

[Example] 1 minutes per hour executes the Bin/account this instruction in the user directory:
1 * * * * bin/account

[Example] every 3:20 A.M. executes two instructions (each with; delimited) as shown below in the user directory:
3 * * * (/bin/rm-f expire.ls logins.bad;bin/expire$#@62;expire.1st)

[Example] in January and April of each year, 4th to 9th, 3:12 and 3:55, executes/bin/rm-f expire.1st This instruction and adds the result after mm.txt this file (mm.txt file is located in the user's own directory location).
12,55 3 4-9 1,4 */bin/rm-f expire.1st$#@62;$#@62;mm.txt

[Example] Let's look at a super user's crontab file:
#Run the ' Atrun ' program every minutes
#This runs anything that's due to run from ' at '. ' at ' or ' atrun '.
0,5,10,15,20,25,30,35,40,45,50,55 * * * */usr/lib/atrun
7 * * * updatedb
8,10,22,30,39,46,54,58 * * * * /bin/sync

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.