Crontab belongs to the common automated Operations Command
Installation and operation
Apt-get install Cronservice cron start #启动cron服务service cron status #查看cron服务状态cron start/running, process 8876
Help information for using cron
man crontab #详述了crontab的语法 crontab (1) General Commands Manual crontab (1) name crontab - maintain crontab files for individual users (Vixie Cron) SYNOPSIS crontab [ -u user ] file crontab [ -u user ] [ -i ] { -e | -l | -r }DESCRIPTION crontab is the program used to install, deinstall or list the tables used To drive the cron (8) daemon in Vixie Cron. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. if the /etc/ Cron.allow file exists, then you must be listed (One user per line) therein in order to be allowed to use this command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use this command. if neither of these Files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use this Command, or all users will be able to use this command. if both files exist then /etc/cron.allow Takes precedence. which means that /etc/cron.deny is not considered and your user must be listed in /etc/cron.allow in order to be able to use the crontab. regardless of the existance of any of these files, the root administrative user is always allowed to setup a crontab. For standard Debian systems, all users may use this com‐ mand. if the -u option is given, it specifies the name of the user whose crontab is to be used (when listing) or modified (when editing) . if this option is not given, crontab exam‐ ines "Your" crontab, i.e., the crontab of the person executing the command.  NOTE THAT SU (8) can confuse crontab and that if you ARE RUNNING INSIDE OF SU (8) you should always use the -u option for safety ' s sake. the first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename '-' is given. The -l option causes the current crontab to be displayed on standard output. see The note under debian specific below. the -r option causes the current crontab to be removed. The -e option is used to edit the current crontab using the editor specified by the visual or editor environment variables. after you exit from the editor, the modified crontab will be installed automatically. if neither of the Environment variables is defined, then the default editor /usr/bin/editor is used. the -i option modifies the -r option to prompt the user for a ' y/y ' response before actually Removing the crontab. debian specific the "Out-of-the-box" behaviour for crontab -l is to display the three line "Do not edit this file " header that is placed at the beginning of the crontab when it is installed. The problem is that it makes the sequence crontab -l | crontab - non-idemPotent -- you keep adding copies of the header. this causes pain to scripts that use sed to edit a crontab. therefore, the default behaviour of the -l option has been
Usage Analysis
crontab [-u user] File #加载crontab任务时加载file文件 accept the typed command if you do not specify a file crontab [-u user] [-i] {-e |-l |-r}crontab-l #显示系统c Ron task Crontab-l-u username #显示用户username的cron任务crontab-e #编辑当前用户的cron任务crontab-e-U root #编辑root用户cron任务crontab-R #删除当 Pre-user cron Task Crontab-r-u username #删除用户username全部cron任务crontab-ir #删除全部cron任务且无需确认
Add Crontab Task
It is recommended that you add system-level cron tasks directly to avoid bugs that are caused by different users
Edit a cron file
sudo vi /etc/crontab #开始编辑 # /etc/crontab: system-wide crontab# unlike Any other crontab you don ' t have to run the ' crontab ' # command to install the new version when you edit this file# and files in /etc/cron.d. These files also have username Fields,# that none of the other crontabs do. shell=/bin/shpath=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user command17 * * * * Root cd / && run-parts --report /etc/cron.hourly25 6 * * * root test -x /usr/sbin/anacron | | ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron | | ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/ sbin/anacron | | ( cd / && run-parts --report /etc/cron.monthly ) 0 2 * * * root find /home/svnuser/ csvn/data/dumps/ -type f -mtime +7 -exec rm -f {} \; #delete Overtime old dump.zip by xxxx 2016-9-22 10:53:29
Add a cron task to the last line at the end of the comment
# m H Dom Mon Dow user command0 2 * * * root find/home/svnuser/csvn/data/dumps/-type f-mtime +7-exec rm-f { } \;
Parsing: At 2:0 A.M. every day, delete more than 7 days of zip files in svn backup with root privileges, manual is a hassle, cron is good for
# m H Dom Mon Dow user Command | | | | | | |--command to be executed | | | | | |--corresponds to the user executing the command, such as Root/username | | | | |--week 0~7, can be multi-choice, such as the Monday and Thursday, the Sunday 0/7 synonymous, * mean weekly | | | |--month 1~12,* = Monthly | | |--Day 1~31,* means daily | |--hour 1~23,0 means 0 points |--minutes 1~59,* or */1 means every minute
Save after configuration is complete, restart cron service
sudo service cron restart
View cron Tasks
sudo crontab-l #此时不会显示系统级cron任务, show only user level, please refer to crontab-e command settings
PS: In some cases, cron requires an environment variable when executing a task, and you need to run the environment variable before running the command.
Finish
2016-9-22
Ubuntu Set timed crontab task