1.命令功能
通过crontab 命令,我们可以在固定的间隔时间执⾏指定的系统指令或 shell script脚本。时间间隔的单位可以是分钟、⼩时、⽇、⽉、周及
2.常用命令参数
-e 编辑某个用户的crontab文件内容
root@localhost:~# crontab -e
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
-l 显示某个用户的crontab文件内容
root@localhost:~# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
-r 从/var/spool/cron⽬录中删除某个⽤户的crontab⽂件,如果不指定⽤户,则默认删除当前⽤户的crontab⽂件。(删除的是当前用户的所有定时任务)
root@localhost:~# ls /var/spool/cron/crontabs/root
/var/spool/cron/crontabs/root
root@localhost:~# crontab -r
root@localhost:~# ls /var/spool/cron/crontabs/root
ls: cannot access '/var/spool/cron/crontabs/root': No such file or directory
3.添加命令格式
minute: 表示分钟,可以是从0到59之间的任何整数。
hour:表示小时,可以是从0到23之间的任何整数。
day:表示日期,可以是从1到31之间的任何整数。
month:表示月份,可以是从1到12之间的任何整数。
week:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。
星号(*):代表每的意思,例如month字段如果是星号,则表示每月都执行该命令操作。
逗号(,):表示分隔时段的意思,例如,“1,3,5,7,9”。
中杠(-):表示一个时间范围,例如“2-6”表示“2,3,4,5,6”。