Linux crontab Command Details (with configuration file path)

Source: Internet
Author: User

Edit/etc/crontab File Configuration cron

Cron service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:


Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
home=///user-run path, here is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly
#####################################################################################################

In Linux, a task can be configured to run automatically for a specified time period, a specified date, or a system average load below a specified quantity. 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 more.

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

37.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 will not be executed.

To use the Cron service, you must have the Vixie-cron RPM package installed and you 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.

37.1.1. Configuring Cron Tasks

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

Shell=/bin/bash path=/sbin:/bin:/usr/sbin:/usr/bin mailto=root home=/# run-parts * * * * Root Run-parts/etc/cron.hour LY 4 * * * Root run-parts/etc/cron.daily 4 * * 0 root run-parts/etc/cron.weekly 4 1 * * Root Run-parts/etc/cro N.monthly

The first four rows are variables that you can use to configure the Cron task runtime environment. The value of the shell variable tells the system which shell environment to use (in this case, the bash shell), and 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 home directory to use when executing a command or script.

Each line in the/etc/crontab file represents a task, in the form of:

Minute hour day Month DayOfWeek command

minute-minutes, any integer from 0 to 59

hour-hours, any integer from 0 to 23

day-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 use the English abbreviations of the month such as Jan, Feb, etc.)

Dayofweek-week, any integer from 0 to 7, where the 0 or 7 represents Sunday (or use the English abbreviations of the week such as Sun, Mon, etc.)

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

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

The short line between integers (-) specifies an integer range. For example,1-4 means integers 1, 2, 3, 4.

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

A forward slash (/) can be used to specify the interval frequency. Adding /<integer after the range > means that integers can be skipped within the range. For example,0-59/2 can be used to define every two minutes in the Minutes field. The interval frequency value can also be used with asterisks. For example, the value of*/3 can be used in the month field to indicate that the task runs every three months.

The line starting with the pound sign (#) is a comment and will not be processed.

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

If a cron task needs to be executed according to the schedule instead of hourly, daily, weekly, or monthly execution, it can be added to the/ETC/CRON.D directory. All files in this 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 3 * Mon Cat/proc/meminfo & Gt;>/tmp/meminfo # Run custom Script The first day's every month at 4:10AM 4 1 * */root/scripts/backup.sh

Example 37-1. Examples of crontab

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 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 user's crontab using the editor specified by the visual or editor environment variable. The file uses the same format as the/etc/crontab. When a change to crontab is saved, the crontab file is saved according to the user name and written to the file/var/spool/cron/username .

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

37.1.2. Controlling the use of cron

/etc/cron.allow and/etc/cron.deny files are used to limit the use of cron. The two formats used by the control file are one user per line. None of the two files allow spaces. 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 what is specified in the 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.

37.1.3. Starting and Stopping 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.

-----

Linux--crontab use of the detailed

Name: crontab
Usage 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]-Delete the user's current crontab.
Crontab-c dir-Specifies the directory for crontab.
Format of the crontab file: M H d M D cmd.
Basic format:
* * * * * command
Time-sharing Weekly command
M: Minutes (0-59). Every minute with * or */1
H: Hours (0-23). (0 means 0 points)
D: Day (1-31).
M: Month (1-12).
D: Day of the Week (0~6,0 is Sunday).
CMD to run the program, the program is fed into sh execution, this shell only user,home,shell these three environment variables
Description
Crontab is used to allow a user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user refers to the setting of the time table for the specified user, which presupposes that you must have permission (for example, root) to specify another's time schedule. If you do not use the-u user, it means setting your own schedule.

Parameters:
CRONTAB-E: Do a text editor to set the time table, the default text editor is VI, if you want to use a different text editor, you first set the VISUAL environment variables to specify the use of the text editor (for example, setenv visual Joe)
Crontab-r: Delete the current schedule table
Crontab-l: List the current schedule
crontab file [-u user]-replaces the current crontab with the specified files.
The format of the schedule table is as follows:
F1 F2 F3 f4 f5 program
Where F1 is expressed in minutes, F2 represents hours, F3 represents the day of the month, F4 represents the month, and F5 represents the day of the one week. Program represents the procedure to be executed.
When F1 is * indicates that every minute the program,f2 is executed, the program is executed every hour, and the remainder of the analogy
When the F1 is a-B, it is executed from the time of the minute to the minute of the hour, and the F2 is a-B indicating that it is executed from the first to the first, and the remainder of the analogy
When F1 is */n, it is performed once every n minutes, and F2 is */n for every n-hour interval, with the remainder of the other analogy
When F1 is a, B, C,... A, B, C,... Minutes to execute, F2 for a, B, C,... The time indicated that the first, B, c ... Hours to execute, and the rest of the analogy
The user can also store all the settings in the file file, using the crontab file to set the time schedule.

Example:
#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS
In December, the/usr/bin/backup is performed every 3 hours in the morning from 6 to 12.

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

Monday to Friday every 5:00 send a letter to [email protected]:
0 * * 1-5 mail-s "HI" [email protected] </tmp/maildata
Every day of the month 0:20 midnight, 2:20, 4:20 .... Perform 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 what the program is doing, and if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line is empty.

Example 2:
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * Date
#晚上11点到早上8点之间每两个小时, 8 in the morning.
0 23-7/2,8 * * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 * mon-wed date
#1月份日早上4点
0 4 1 Jan * Date
Example
$crontab-L lists the user's current crontab.

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


Some examples of crontab files:

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

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows that 4:45 restarts Apache on the 1, 10, and 22nd of the month.

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

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

0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the restart of Apache every Saturday at 11:00am.

* */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-point restart from Monday to Wednesday Apache

0 4 1 Jan */usr/local/etc/rc.d/lighttpd restart
4-point restart of Apache on January 1

Example:

Each of the two time values is separated by commas.
In addition to the numbers there are several special symbols that are "*", "/" and "-", ",", * represent all the values within the range of numbers, "/" for each meaning, "*/5&prime;" represents every 5 units, "-" represents a number to a number, "," separate several discrete numbers.


Every morning at 6.

0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root.

Every two hours

0 */2 * * echo "has a break now." >>/tmp/test.txt

Every two hours between 11 o'clock and 8 in the morning, eight in the morning.

0 23-7/2,8 * * * echo "a good Dream:)" >>/tmp/test.txt

Every month, number 4th and Monday to Sunday, three a.m., 11.

0 4 * 1-3 command line

January 1 morning, 4.

0 4 1 1 * command line

After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and can only be edited with CRONTAB-E. After Cron starts, read the file once every one of the clocks, and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.

2. Edit the/etc/crontab file configuration cron

Cron service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:

Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
home=///user-run path, here is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly



Attention to the "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.


5,15,25,35,45,55 16,17,18 * * command

This means that any day any month, in fact, the daily 4 o'clock in the afternoon, 5 points, 6 points of 5 min, min, min, min, min, min, when the execution of orders.

[Example] in every Monday, three, five 3:00 the system enters the maintenance state and restarts the system. Then 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 execute the Innd/bbslin in the user directory:

10,40 * * * * Innd/bbslink

[Example] 1 minutes per hour execute the bin/account command in the user directory:

1 * * * * bin/account

[Example] every day 3:20 A.M. executes two instructions (each instruction is separated) as follows in the user directory:

3 * * * (/bin/rm-f expire.ls logins.bad;bin/expire$#@62;expire.1st)

[Example] every year in January and April, 4th to 9th 3:12 and 3:55 execute/bin/rm-f expire.1st This command, and add the results 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 crontab file for a super User:

#Run the ' Atrun ' program every minutes

#This runs anything that's due to run from the ' at '. See mans ' 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

Linux crontab Command Details (with configuration file path)

Related Article

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.