crontab属于常见的自动化运维命令
安装及运行
apt-get install cron service cron start #启动cron服务 service cron status #查看cron服务状态 cron start/running, process 8876
使用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
用法解析
crontab [ -u user ] file #加载crontab任务时加载file文件,如果不指定文件则接受键入的命令 crontab [ -u user ] [ -i ] { -e | -l | -r } crontab -l #显示系统cron任务 crontab -l -u username #显示用户username的cron任务 crontab -e #编辑当前用户的cron任务 crontab -e -u root #编辑root用户cron任务 crontab -r #删除当前用户cron任务 crontab -r -u username #删除用户username全部cron任务 crontab -ir #删除全部cron任务且无需确认
添加crontab任务
建议直接添加系统级cron任务以避免不同用户导致的不执行的bug
编辑cron文件
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/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 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
在末尾注释上一行添加cron任务
# m h dom mon dow user command 0 2 * * * root find /home/svnuser/csvn/data/dumps/ -type f -mtime +7 -exec rm -f {} \;
解析:在每天的凌晨2点0分,以root权限删除svn备份中的超过7天的zip文件,手动很麻烦,cron很适合
# m h dom mon dow user command | | | | | | |--所需执行的命令 | | | | | |--对应执行命令的用户,比如root/username | | | | |--星期0~7,可以多选,比如1,4-表示周一及周四,周日0/7同义,,*表示每周 | | | |--月份1~12,*表示每月 | | |--日起1~31,*表示每日 | |--小时1~23,0表示0点 |--分钟1~59,*或者*/1表示每分钟
配置完成后保存,重启cron服务
sudo service cron restart
查看cron任务
sudo crontab -l #此时不会显示系统级cron任务,只显示用户级,请参考crontab -e命令设置
ps:部分情况下,cron在执行任务时需要环境变量,此时需要在运行命令之前先运行环境变量。
完
2016-9-22
时间: 2024-11-05 21:56:42