crontab command

cron: Periodically scheduled task, the task cycling plans, mainly for backing up data.
      crontab  
      Usage: crontab [-u the User] [-e | the -l | -r]
      -u <the User> Specifies the user the DEFINE the User #
      -e edit user's crontab # edit scheduled tasks
      -l list user's crontab # View Scheduled Tasks
      -r delete user's crontab # delete a scheduled task
1, crontab -e to edit the contents of a periodic scheduled task
2, view the configuration: cat / etc / crontab
time
min hour day month week command
-minute hour day week month action (task commands)    
0-590- 231-311-120-7 command or script (write absolute path)
all the documents 30 2 1 * * rm -rf / tmp / * // delete the number at 2:30 monthly 1 / tmp directory
5 * * 6 3 rm -rf / tmp / * // all files under five minutes to delete three hour a week in June each year the / tmp directory
All files 6 6 * 6 6 rm -rf / tmp / * // 6 minutes at 6:00 deletion Saturday in June each year the / tmp directory


* Any time
- period of time // 8-11 * * * * rm -rf / tmp / * // under 8 minutes 9 minutes 10 minutes 11 minutes per hour of deleted the / tmp directory of all files     
, separate (discrete time) All files // 1,2,5,9 * * * * rm -rf / tmp / * // 1 minute 2 minutes 5 minutes 9 minutes per hour of deleted the / tmp directory     
0-4,8-12 //
0 -23/2 interval // 1 0-23 / 2 * * * rm -rf / tmp / * // "0,2,4,6,8,10,12,14,16,18,20,22 ").
* / 2 every two hours // 1 * / 2 * * * rm -rf / tmp / * rm command executed every 2 hours


*  0-23  *  *  *  rm -rf /tmp/*   
*   */1   *  *  *  rm -rf /tmp/*   


3, the wording of time
The time and date fields are:


      field          allowed values
      -----          --------------
      minute         0-59
      hour           0-23
      day of month   1-31
      month          1-12  (or  names,
      see below)
      day  of  week    0-7  (0 or 7 is
      Sun, or use names)


Case I: every Wednesday at 10:00 pm backup / etc / directory to the / tmp directory called 2017-04-12_etc.tar.gz backup file contains today's date
crontab -e
* 22 * 3 * / usr / bin / tar cvzf / tmp / `/ usr / bin  / date +% F`_etc.tar.gz / etc
Note:` `the` `command to replace the function of the command, the system first performs` `(trans prime backticks) of command. Can also be written as `` $ ()
Case II: Keep only the last one month of the log file
crontab -e
1 * * * * the Find / var / log -mtime +30 | xarge -i RM -rf {}
Case 3: monthly No. 1 2:30 empty the temporary disk
crontab -e
30 2 1 * * / usr / bin / RM -rf / tmp / * 
case four: Every day 8:10 off the computer at night, except Saturdays, Friday Sundays 17:10 Turn off the computer
crontab -e
10 20 * * 1-4 / usr / sbin / poweroff
10 * 17 * 5, 7 / usr / sbin / poweroff
case 5: jim user at home every day to create a directory named after the date directory 20,170,412
the crontab -e -u Jim
. 1 0 * * * / bin / mkdir / Home / Jim / `DATE the Y% +% m% d`


4, periodically query the contents of the scheduled tasks
      crontab -l # to view the current user
      crontab -u jim - # View the specified user
5, periodically delete all scheduled tasks a user (delete a scheduled task crontab -e to enter text deleted)
      crontab -r # current user
      crontab -u jim -r # specified user


System-defined scheduled task (default)
      updates the whatis database
      update updatedb database
      log periodic rotation roll (log cutting)
      to clean / tmp / var / tmp
      state information collection system
 
vim / etc / crontab configuration file        
# For details see man 4 crontabs


Example of the Job Definition #:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23 is)
# | | .---------- month The Day of (. 1 - 31 is)
# | | | .------- month The (. 1 - 12 is) OR Jan, On Feb,-Mar, On Apr ...
# | | | | .---- Day of Week (0 -. 6) (or the Sunday = 0. 7) oR Sun, Mon, Tue, Wed, Thu, Fri, SAT
# | | | | |
# * * * * the User-* name the command to BE the executed  
* / 5 * * * * root command // every five minutes to execute commands as root


Scripts can exist within the script directory will be in accordance with the following time to execute
      cd / etc
      cron.hourly / # per hour
      cron.weekly / # weekly
      cron.daily / # day
      cron.monthly / # month


6, unused files deleted more than a month
      CAT /etc/cron.daily/tmpwatch
7, crontab Service Management:
      Are search software is installed: rpm -q crontabs
      list file search software: rpm -ql crontabs
      check software configuration files list: rpm -qc crontabs
      service status: systemctl status crond Note: d is the daemon daemon
      to restart at the service: systemctl restart crond service crond restart or
      shut down at service: systemctl stop crond or service crond stop
      the boot does not start at the service: systemctl disable crond or chkconfig crond off
      the boot at the service: systemctl enable crond crond oN or chkconfig
8, cron rights management
/etc/cron.allow white list: allows only users in this list using crontab (the file itself does not exist)
/ etc / cron.deny blacklist: users in this list can not use crontab
      Note: neither file exists, only the root user can use crontab
             Two files are present, allow the implementation of priority

---------------------
Disclaimer: This article is CSDN blogger "coisini_ Mi" in the original article, follow the CC 4.0 by-sa copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/qq_34889607/article/details/78635141

Guess you like

Origin www.cnblogs.com/2567xl/p/11326234.html