Regular tasks crond, crontab

 

crontab command is a configuration command to set the timing task rule, the file name of the current user configuration file / etc / crontab and / var / spool / cron / directory.
  -l displays the current regular tasks
  -e to edit scheduled task


For root:
the crontab -l equivalent CAT / var / spool / the cron / root
the crontab -e equivalent vim / var / spool / cron / root

 

/ Etc / crontab configuration file introduction:

[root@55test /etc]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- 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
# | | | | |
# * * * * * user-name command to be executed
  • * The first column: min (0-59)
  • * The second column: When (0-23)
  • * Third row: Day (1-31)
  • * The fourth column: Month (1-12)
  • The fifth column *: Week (0-6)

Notation:

 

  • - represents a continuum, for example: 1-10 may represent a 1:00 to 10:00
  • For representing discrete sections, such as: 1,3,9,22 may represent a 1:00, 3:00, 9:00, 22:00
  • / N for the time interval, such as: / 5 may represent every five minutes or every 5 hours, etc.
  • Tasks performed at the timing specified user
  • In the regular tasks and scripts, command the full path format
  • (%) / Etc / crontab, the special symbol for an escape character; writing a script file, you do not need to use an escape character
  • Before the scheduled task scripts need to add / bin / sh
  • Not free to print output, the timing of the end of the task added &> / dev / null (> / dev / null 2> & 1), to avoid regular mail to the root user tasks, to prevent a large amount of information being given using excessive inode.
  • Regular tasks using shell nonlogin execution, the user login shell environment variables may be different, PATH, PS1 variables need to re-export.

For example

time:

* / 1 * * * * / bin / sh /scripts/data.sh # executed every one-minute 
30 3,12 * * * / bin / sh /scripts/boy.sh # 3: 30 to 12:30 executed 
30 * / 6 * * * / bin / sh /scripts/boy.sh # 6 hours slightest executed every 
30 8-18 / 2 * * * / bin / sh /scripts/boy.sh # 8-18 per slightest performed every 2 hours 
30 21 * * * # 21:30 daily performed 
45 4 1,10,22 * * / application / apache / bin / apachectl graceful # 1,10,22 each month performed 4:45 
10 1 * * 6,0 / application / apache / bin / apachectl graceful # every Saturday and Sunday 1:10 execution 
0,30 18-23 * * * / application / apache / bin / apachectl graceful # 18-23 points of between the whole point, the slightest perform 
00 * / 1 * * * / application / apache / bin / apachectl graceful # hour the whole point of execution

Common mistakes:

* 23,0-7 / 1 * * * / application / apache / bin / apachectl graceful # between 23:00 and 0-7 point run every minute

command:

[55test the root @ ~] # the crontab -l 
* / 2 * * * * / usr / bin / ZCF the tar / mnt / mm_`date + \% F. _ \% & T`bak1.tz mm *> / dev / null 
* /. 3 * * * * / usr / bin / the tar ZCF / mnt / mm _ $ (DATE + \% F. _ \% T) bak2.tz mm * &> / dev / null 
* /. 5 * * * * / bin / SH & /work/sh-file/mm_bak.sh> / dev / null 
[root @ 55test / Work / SH-File] # vi mm_bak.sh 
/ usr / bin / cd / root && / usr / bin / tar ZCF / mnt / mm _ $ (date +% F_ % T) bak3.tz ./mm* # && used to associate two commands, after performing the first successful, then execute the second command 
may use the following format: 
/ usr / bin / cd / root && \ # use \ newline, easy to read 
/ usr / bin / tar zcf / mnt / mm _ $ (date +% F_% T) bak3.tz ./mm*

  

Commissioning scheduled task ideas:
1, View error log / var / log / cron, can first clearing the log (echo> / var / log / cron) before being given a new View
2, adjust the system date and time (interval greater than 5 minutes), the timing task time task execution frequency or timing, speed up command.
3, the script log output, debug regular tasks, / bin / sh /server/scripts/bak.sh &> / tmp / bak.log

 

Guess you like

Origin www.cnblogs.com/00huajiang/p/11363330.html