CentOS crontab timed task design


At the command task is run only once, cycling routine planning tasks, Linux systems is by the cron (crond) to control this system service. Linux system has an awful lot of planning work, so this service is started by default. In addition, users can also set up a scheduled task, so Linux system also provides user scheduled tasks command: crontab command.
A, crond profile
Crond is Linux Xia used to periodic of implementation a species task or waiting for processing some event of a guardian process, and Windows Xia of plans task similar, dang installation completed operating system Hou, default will installation this service tool, and will automatically started crond process, crond process each minutes will regularly check whether has to implementation of task, if has to implementation of task, is automatically implementation the task.
Task scheduling under Linux is divided into two categories, scheduling system tasks and user tasks scheduling.
System Task Scheduler: System periodically to perform work, such as write cache data to the hard disk, logging, etc. In the/etc directory there is a crontab file, the task schedule configuration file is System.
/Etc/crontab file contains the following lines:
[root@localhost ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=””HOME=/
# run-parts
51 * * * * root run-parts /etc/cron.hourly
24 7 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
[root@localhost ~]#
Qian four line is used to configuration crond task run of environment variable, first line SHELL variable specified has system to using which shell, here is bash, second line PATH variable specified has system implementation command of path, third line MAILTO variable specified has crond of task implementation information will through e-mail sent to root user, if MAILTO variable of value for empty, is said not sent task implementation information to user, Last line the HOME variable to specify the command or script to use home directories. Sixth to Nineth line indicate in detail in the next section. Here is not said.
Task scheduling in user: the user of the work to be performed on a regular basis, such as users, such as data backup, scheduled email alerts. Users can use crontab to customize their own scheduled tasks. All userdefined crontab files are stored in the/var/spool/cron directory. Its file name and user name.
User permissions file:
File:
/etc/cron.deny
Introductions:
The users listed in the files does not allow using the crontab command
File:
/etc/cron.allow
Introductions:
The users listed in the files using the crontab command
File:
/var/spool/cron/
Introductions:
All users crontab files store directory, user name
Meaning of the crontab file:
User created the crontab file, each row represents a task, each row of each field represents a set, its format is divided into six fields, a former fivetime setting, the sixth is the command to be executed, in the following format:
minute hour day month week command
Of which:
Minute: for minutes, which can be any integer from 0 to 59.
Hour: the hour, which can be any integer from 0 to 23.
Day: date, which can be any integer from 1 to 31.
Month: month, which can be any integer from 1 to 12.
Week: that day of the week, which can be any integer from 0 to 7, where 0 or 7 for Sunday.
Command: command to execute, it may be a system command, or it can be to write your own script file.
In each of these fields, you can also use the following special characters:
An asterisk (*): represents all possible values, such as if the month field is an asterisk, then in the other field conditions every month after the command operation.
A comma (,): you can specify a list of values separated by commas, for example, 1,2,5,7,8,9
Slash (): you can use an integer between bar represents a range of integers, for example, 2-6 means 2,3,4,5,6
Forward slash (/): can use forward slashes to specify the time interval of frequencies, for example, 0-23/2 means once every two hours. Also use forward slash and asterisk, for example, */10, if you are using in the minute field, say once every ten minutes.
Second, crond service
Install crontab:
yum install crontabs
Service instructions:
/Sbin/service service crond start//start
/Sbin/service service crond stop/on/off
/Sbin/service restart crond restart//services
/Sbin/service crond reload//reload configuration
See crontab service status:
service crond status
Crontab service manually:
service crond start
See crontab service is already set to boot, execute the command:
ntsysv
Added Auto start:
chkconfig –level 35 crond on
Third, the crontab command xiangjie
1. Command format:
crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
2. Command function:
With the crontab command, we can be performed at regular intervals the specified system command or shell script script. Interval units can be minutes, hours, days, months, weeks, and any combination of the above. This command is set up periodic log analysis or data backups, and so on.
3. Command parameter:
-U user: used to set a user‘s crontab service, for example, -u ixdba set ixdba user‘s crontab service, this parameter has a root user to run.
File:file is the name of a command file, says it will file for the task list and load the file crontab crontab. If this file is not specified on the command line, crontab command accepts standard input (keyboard) you type on the command, and load them into crontab.
-E: Edit the contents of a user‘s crontab file. If you do not specify a user, you edit the crontab file of the current user.
-L: displays the contents of a user‘s crontab file, if you do not specify a user, then displays the contents of current user‘s crontab file.
-R: a user‘s crontab file is removed from the/var/spool/cron directory, if you do not specify a user, the default current user‘s crontab file.
-I: delete confirmation prompt when the user‘s crontab file.
4. Commonly used methods:
1.) to create a new crontab file
Taking into account before submitting a crontab file for the cron process, first thing to do is to set the environment variable EDITOR. Cron process according to its edit the crontab file to determine which editor to use. 9 9% use VI for UNIX and LINUX users, if you do too, then you edit under your $ HOME directory. Profile file, add a line that says:
EDITOR=vi; export EDITOR
Then save and exit. Might as well create a <user> cron file, which <user> is a user name, such as davecron. Add the following contents in the file.
# (put your own initials here)echo the date to the console every
# 15minutes between 6pm and 6am
0,15,30,45 18-06 * * * /bin/echo ‘date’ > /dev/console
To save and exit. Sure front 5 fields separated by a space.
In the above example, the system will output to the console every 15 minutes current time. If the system crashes or hangs, the time can be displayed from the last at a glance is the system stopped working. In some systems, by the tty1 console, according to the above example is modified accordingly. In order to submit your crontab file you just created, you can put this newly created file as a cron command parameters:
$ crontab davecron
Now that the file has been submitted to cron processes, which will run once every 15 minutes.
Meanwhile, a copy has been placed in the newly created file in the/var/spool/cron directory, the file name is the user name (that is, Dave).
2.) list the crontab file
In order to list the crontab file, you can use:
$ crontab -l
0,15,30,45,18-06 * * * /bin/echo ‘date’ > dev/tty1
And you will see something similar to the above. You can use this method in $ h o m e a backup to crontab files in a directory:
$ crontab -l > $HOME/mycron
That way, if you accidentally deleted the crontab file, you can use the methods described in the previous section quickly restored.
3). editing crontab files
If you want to add, delete or edit entries in crontab file, environment variable is set to e d I TO r v I v I can be used to edit a crontab file, the corresponding command is:
$ crontab -e
V I edit can be used like any other files that modify the crontab file and exit. If you have modified some of the entries, or add a new entry, when you save the file, c r o n be the necessary integrity checks. If one of these domains have value outside the allowed range, it will alert you.
When we edit the crontab file, maybe I‘ll add a new entry. For example, add the following rule:
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;
Now save and exit. Best on every entry in the crontab file to add a comment, so that you can know its function, running time, is even more important, know which user it is homework.
Let us now using the preceding command to list all its information:
$ crontab -l
# (crondave installed on Tue May 4 13:07:43 1999)
# DT:ech the date to the console every 30 minites
0,15,30,45 18-06 * * * /bin/echo ‘date’ > /dev/tty1
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;
4). removing crontab files
To delete a crontab file, you can use:
$ crontab -r
5.) recover lost crontab files
If you accidentally deleted the crontab file, if you own $ h o m edirectories also have a backup, you can copy it to the/var/spool/cron/<username>, which <username> is the user name. If due to a permissions problem cannot complete the copy, you can use:
$ crontab <filename>
Where <filename> is your $ h o m ecopy of the files in a directory name.
I suggest you own $ h o m e saving a copy of the file in the directory. I had a similar experience, mistakenly deleted the crontab files a number of times (close to the right of the e r key). This is why some system documentation recommends against directly editing crontab files, but editing a copy of the file, and then resubmit the new file.
Crontab variants of some spooky, so use caution when using the crontab command. If you missed any options, crontab may open an empty file, or looks like a blank file. Then press DELETE key to exit, do not press <Ctrl-D> or you will lose the crontab file.
5. Use examples
Example 1: every 1 minutes to perform a command
Command:
* * * * * command
Example 2:3rd per hour and 15 minutes
Command:
3,15 * * * * command
Example 3:3rd at 8 o’clock in the morning to 11 points and 15 minutes
Command:
3,15 8-11 * * * command
Example 4: every day at 8 o’clock in the morning to 11 points in the 3rd and 15 minutes
Command:
3,15 8-11 */2 * * command
Example 5: every 3rd Monday morning at 8 o’clock in the morning to 11 points and 15 minutes
Command:
3,15 8-11 * * 1 command
Example 6: night 21:30 restart the SMB
Command:
30 21 * * * /etc/init.d/smb restart
Example 7:1, 10, 22nd of each month 4:45 restart the SMB
Command:
45 4 1,10,22 * * /etc/init.d/smb restart
Example 8: every Saturday and Sunday 1:10 restart the SMB
Command:
10 1 * * 6,0 /etc/init.d/smb restart
Example 9: daily every 30 minutes between 18:00 to 23:00 restart the SMB
Command:
0,30 18-23 * * * /etc/init.d/smb restart
Example 10:11:00 pm every Saturday night to restart the SMB
Command:
0 23 * * 6 /etc/init.d/smb restart
Example 11: every hour to restart the SMB
Command:
* */1 * * * /etc/init.d/smb restart
Example 12:11 o’clock in the evening until 7 in the morning, between every two hours to restart the SMB
Command:
* 23-7/1 * * * /etc/init.d/smb restart
Example 13: the 4th of each month and every Monday to Wednesday 11 restart SMB
Command:
0 11 4 * mon-wed /etc/init.d/smb restart
Example 14: January 1 4 restart SMB
Command:
0 4 1 jan * /etc/init.d/smb restart
Example 15: hourly/etc/cron.hourly directory script is executed
Command:
01 * * * * root run-parts /etc/cron.hourly
Introductions:
This parameter is run-parts, if this parameter is removed, you can write a script to run behind, instead of the directory name
Four, use precautions
1. pay attention to environment variable problem
Sometimes we create a crontab, but this task cannot be executed automatically, and perform this task manually is no problem, this situation is usually caused due to not configured the environment variable in the crontab file.
In crontab file in the defined multiple scheduling task Shi, need special note of a problem is environment variable of set, because we manual implementation a task Shi, is in current shell environment Xia for of, program certainly can found environment variable, and system automatically implementation task scheduling Shi, is not loaded any environment variable of, so, on need in crontab file in the specified task run by needed of all environment variable, such, system implementation task scheduling Shi on no problem has.
Do not assume that cron knows the special circumstances required, it in fact does not know. So you need to keep in the shelll script provides all necessary paths and environment variables, except for some global variable is automatically set. So pay attention to the following 3 points:
1) involving file paths in the script writing global path;
2) scripts using Java or other environment variables, by source order into an environment variable, such as:
cat start_cbp.sh
#!/bin/sh
source /etc/profile
export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf
/usr/local/jboss-4.0.5/bin/run.sh -c mev &
3) when manually executing the script OK but crontab does not execute. When must bold environment variable is suspected to blame, directly into the environment variable in the crontab and can try to solve the problem. Such as:
0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh
2. takes note of cleaning system the user‘s mail log
Completion of each task scheduling, the system outputs the task information is sent via e-mail to the current users of the system, so that over time, log information will be very large, and may affect the normal operation of the system and, therefore, to redirect every task is important.
For example, you can set in the crontab file as follows, ignoring the log output:
0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1
/Dev/null 2>&1 to redirect standard output to/dev/null, and then redirecting standard error into standard output, standard output is redirected to/dev/null, so the standard error will be redirected to/dev/null, so that the log output problem is solved.
3. user-level and system-level task scheduling job scheduling
System level task scheduling main completed system of some maintenance operation, user level task scheduling main completed user since defined of some task, can will user level task scheduling put to system level task scheduling to completed (not recommends so do), but in turn is no, root user of task scheduling operation can through “crontab – uroot – e” to set, also can will scheduling task directly writes/etc/crontab file, need note of is, If you want to define a scheduled task to restart the system, tasks must be placed into the/etc/crontab file, even under the root user to create a scheduled task to restart the system is invalid.
4. other considerations
New cron job, not executed immediately, at least take 2 minutes to perform. If you restart the cron is immediately performed.
When crontab suddenly fails, you can try/etc/init.d/crond restart to solve the problem. Or view the log for a job ever executing/error/var/log/cron.
Don’t run. It from the Crontab directory (/var/spool/cron) removes the user‘s Crontab file. Delete all of the user‘s crontab is gone.
% Have special meanings in crontab, represent a new line meant. Must be escaped if used \%, such as date for regular ‘+%Y%m%d’ in crontab will not be executed, should be replaced by date ‘+\%Y\%m\%d’.