crond:是Linux系统中提供了计划任务的一个守护进程。
crontab 给 crond设置计划任务
crontab 给 crond送情报,告诉crond什么时间做什么事情
[root@localhost lianxi]# vim /etc/crontab
# 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
创建计划任务:
[root@localhost lianxi]# crontab -e 创建计划任务 edit
30 3 7 12 * bash /lianxi/backup_log.sh
[root@sanchuang lianxi]# crontab -e
第1步:按i进入计划任务的编辑模式,输入下面的内容
0 23 * * * bash /lianxi/backup_log.sh
第2步:按ESC,再输入:wq 退出并且保存
查看计划任务:
[root@localhost lianxi]# crontab -l 查看有哪些计划任务 list
30 3 7 12 * bash /lianxi/backup_log.sh
删除计划任务:
推荐直接注释掉,不删除。
[root@localhost ~]# crontab -r
如何知道计划任务是否执行?
1.直接看效果
2.看日志
crontab的日志: 记录哪些计划任务的创建、执行、修改、查看等操作
/var/log/cron
[root@sanchuang log]# tail -f cron
Mar 16 15:06:35 sanchuang crontab[22483]: (root) LIST (root) 查看
Mar 16 15:06:49 sanchuang crontab[22484]: (root) BEGIN EDIT (root) 编辑
Mar 16 15:07:07 sanchuang crontab[22484]: (root) REPLACE (root) 修改
Mar 16 15:07:07 sanchuang crontab[22484]: (root) END EDIT (root) 结束编辑
Mar 16 15:07:25 sanchuang crontab[22486]: (root) LIST (root)
Mar 16 15:08:01 sanchuang crond[22151]: (root) RELOAD (/var/spool/cron/root) 加载计划任务开始执行
Mar 16 15:08:01 sanchuang CROND[22491]: (root) CMD (bash /lianxi/sanchuang/create_dir.sh)
Mar 16 15:08:01 sanchuang CROND[22489]: (root) CMDOUT (tar: 从成员名中删除开头的“/”) 脚本的执行输出效果
时间 地点 人物 发生的事情
CROND[22491] CROND 是进程 [22491] 是crond的进程号 pid号
[root@sanchuang log]# ps aux|grep crond
root 22151 0.0 0.1 36332 3624 ? Ss 13:08 0:00 /usr/sbin/crond -n
root 22518 0.0 0.0 12320 976 pts/1 R+ 15:10 0:00 grep --color=auto crond
[root@sanchuang log]#
CMD 表示执行命令 command
存放计划任务的文件夹
/var/spool/cron
用户定义的设置,位于文件:/var/spool/cron/用户名
/etc/下的这些文件夹里可以存放linux系统的计划任务
cron.d/ cron.daily/ cron.deny cron.hourly/ cron.monthly/ crontab cron.weekly/
cron.d/ 存放计划任务的目录
cron.daily/ 存放每天需要执行的任务
cron.hourly/ 存放每小时需要执行的任务
cron.monthly/ 存放每月需要执行的任务
cron.weekly/ 存放每周需要执行的任务
cron.deny 存放拒绝谁执行计划任务