In Linux, periodic tasks are typically handled by the cron daemon Ps-ef | grep cron
Cron reads one or more configuration files that contain the command line and its invocation time.
The cron configuration file is called "crontab" and is shorthand for "cron table".
First, cron find the configuration file (set shell script) in 3 places:
1,/var/spool/cron/yanggang This directory is stored in each user (including root) of the crontab task, each task is named after the creator, such as the user Tom The crontab mission file is/var/spool/cron/Tom .
[Email protected]$ sudo ls-l/var/spool/cron/(or sometimes/var/spool/cron/crontabs/)
-RW-------1 root crontab 1071 2011-09-19 17:20 Root
-RW-------1 Yanggang crontab 1176 2011-09-24 11:07 Yanggang
Typically a user has at most one crontab file (e.g. root, Yanggang, etc.), and its corresponding log is in the/var/spool/mail/root (or/var/spool/mail/yanggang) file
2./etc/crontab This file is responsible for arranging the maintenance system and other tasks crontab by the system administrator.
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
#.----------------Minute (0-59)
# | .-------------Hour (0-23)
# | | .----------Day of Month (1-31)
# | | | .-------month (1-12) OR jan,feb,mar,apr ...
# | | | | .----Day of Week (0-6) (sunday=0 or 7) or
#sun, Mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
For example:
*/5 * * * * Root/usr/libexec/atrun
minute: represents the first part of an hour in the range 0-59.
Hour: represents the first hour of the day, Range 0-23.
Mday: represents the day of the one month, the range 1-31.
Month : represents the month ordinal of a year, with a range of 1-12.
Wday: represents the day of the week, and the range 0-7 (0 and 7 are Sunday).
Who : to use what identity to execute the directive, when you use CRONTAB-E, you do not have to add this field.
command: the command to execute.
3./etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.
Second, the Authority
Crontab permissions issue to/var/adm/cron/next look, file Cron.allow and Cron.deny exist
Use the following:
1. If none of the two files exist, only the root user can use the crontab command.
2, if the Cron.allow exists but Cron.deny does not exist, only the users listed in the Cron.allow file can use the crontab command, if the root user is not inside, then the root user can not use crontab.
3, if the Cron.allow does not exist, Cron.deny exist, only the users listed in the Cron.deny file can not use the crontab command, other users can use.
4, if two files exist, then listed in the Cron.allow file and not listed in Cron.deny users can use crontab, if two files have the same user,
If the user is in the Cron.allow file, you can use the crontab command if there is a user in the Cron.allow.
Normal users in AIX have crontab permissions by default, and if you want to restrict users from using crontab, you need to edit/var/adm/cron/cron.deny
Hp-unix in the default normal user does not have crontab permissions, to release the normal user's crontab permissions can be compiled
Iii. Creating a cron script
The first step: Write a cron script file named Crontest.cron.
15,30,45,59 * * * * echo "xgmtest ..." >> Xgmtest.txt, every 15 minutes, executes the print once command
Step Two: Add a scheduled task. Executes the command "Crontab Crontest.cron". Get
Step three: "Crontab-l" to see if the timed task is successful or if the corresponding cron script is generated under/var/spool/cron
Four, cron service
Cron is a timed execution tool under Linux that can run a job without human intervention.
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
/sbin/service crond Status//view service statuses
Example: use sudo to stop and start the service
[Email protected] $sudo service cron stop
Cron stop/waiting
[Email protected]$ sudo service cron start
Cron start/running, Process 7502
v. Usage of crontab
The crontab command is used to install, delete, or list tables used to drive the cron daemon, and the user places the sequence of commands that need to be executed in the crontab file for execution.
Each user can have their own crontab file,/var/spool/cron under the crontab file can not be directly created or directly modified, the Crontab file is created by the crontab-e command
In the crontab file, how to enter the command and time to execute, each row in the file contains six fields, the first five of which are the time the specified command was executed, and the last field is the command to be executed.
Spaces or tabs are used to separate each field. The format is as follows:
minute hour day-of-month month-of-year day-of-week commands
Legal value 00-59 00-23 01-31 01-12 0-6 (0 is Sunday)
In addition to the numbers there are a few special symbols that are "*", "/" and "-", ",", * representing all the values within the range of numbers
"/" stands for each meaning, "/5" means every 5 units, "-" stands for a number of discrete numbers from a certain number to a digit, ",".
-L Displays the current crontab on the standard output.
-R Deletes the current crontab file.
-e Edit the current crontab file using the editor referred to in visual or editor environment variables.
When you finish editing, the edited file is automatically installed.
A few examples:
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
11 o'clock to 8 a.m. every two hours and eight a.m..
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 Shell=/bin/bash path=/sbin:/bin:/usr/sbin:/usr/bin mailto=root//If an error occurs or there is data output, the data is sent to this account as an email home= /
Execute scripts within/etc/cron.hourly per hour (first minute)
* * * * * Root run-parts/etc/cron.hourly
every day (wee 4:02) executes scripts within the/etc/cron.daily
4 * * * Root run-parts/etc/cron.daily
every week (early Sunday 4:22) Execute scripts within/etc/cron.weekly
4 * * 0 root run-parts/etc/cron.weekly
Monthly (early 1th 4:42) to execute scripts within the/etc/cron.monthly
4 1 * * Root run-parts/etc/cron.monthly
Note: "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.
Every day 4 o'clock in the afternoon, 5, 6 points of 5 min, min, min, min, min, min, when the execution of orders.
5,15,25,35,45,55 16,17,18 * * command
Every Monday, three, five 3:00 the system enters the maintenance state and restarts the system.
* *1,3,5 Shutdown-r +5
10 minutes per hour, 40 minutes execute the innd/bbslin command in the user directory:
10,40 * * * * Innd/bbslink
1 minutes per hour execute the bin/account command in the user directory:
1 * * * * bin/account
Execute two instructions (each instruction is delimited) below the user directory every day 3:20 A.M.:
203 * * * (/bin/rm-f expire.ls logins.bad;bin/expire$#@62;expire.1st)
Each year in January and April, 4th to 9th of 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,553 4-91,4 */bin/rm-f expire.1st$#@62;$#@62;mm.txt
Six, several questions
The Crond process is not running
1. Start the Crond process
/etc/init.d/crond start
2. Boot from Crond process
Chkconfig Crond on
Crontab Edit:
Input Edit command: crontab-e
# m H Dom Mon Dow command
Shell=/bin/bash
* * * * * cd/home/barry/top800/top10/top10_fruits/&&/top10_all.sh
CTRL + O (write)-"Enter" (save file name)--"Ctrl + X (exit)
Enter view command: crontab-l
# m H Dom Mon Dow command
Shell=/bin/bash
* * * * * cd/home/barry/top800/top10/top10_fruits/&&/top10_all.sh (this method is recommended)
Syntax error:
Syntax error: "(" unexpected
See also: Linux-bash Syntax Error
Alternatively, specify the SHELL interpreter (SH) when crontab-e: Shell=/bin/bash (see crontab editing example above)
Path Error:
In/var/spool/crontab/yanggang, the following command was added, prompting for the xxx.sh path not found in the log file/var/spool/mail/yanggang
* * * * */home/barry/top800/top10/top10_fruits/top10_all.sh
Or
* * * * * bash/home/barry/top800/top10/top10_fruits/top10_all.sh
This is because you use absolute paths in crontab to execute script top10_all.sh, so other scripts referenced in script top10_all.sh need to use absolute paths to be crontab found and executed
How to avoid the complex settings of absolute paths, as shown in the above six, several questions , in the following format:
* * * * * cd/home/barry/top800/top10/top10_fruits/&&/top10_ All.sh (This is the recommended way to enter the directory first, and then execute the script; otherwise, the execution script will need to add an absolute path to the other scripts)
Recommended reference:
Linux timed Run command script (Crontab detailed)
Linux Crontab Scheduled Tasks
How to set-up a crontab file
Transferred from: http://blog.csdn.net/ithomer/article/details/6817019
Linux timed Run command script--crontab