linux系统中,按计划重复性的运行一些任务被称为周期性任务。
1.服务(at,cron,anacron)+服务列表(crontab)配置
2.服务启停命令
3.创建示例(用户+系统)
4.小工具链接https://tool.lu/crontab/。
1.服务(at,cron,anacron)+服务列表(crontab)配置
一次 | at | 直接输入:echo "date >> ~/myjob.txt"|at now+3min | ||
交互式:at now+2min | ||||
多次 | cron | cron :读取配置文件 crond: 执行任务 | crontab 任务列表存放路径 | 进程本身的配置文件 |
用户 /var/spool/cron/用户名 备注:只能有一个配置文件 | ||||
系统:/etc/crontab | /etc/cron.allow | |||
其他的:/etc/cron.d | /etc/cron.deny | |||
关机后 | anacron |
2.服务启停命令
systemctl start crond //启动服务
systemctl stop crond //关闭服务
systemctl restart crond //重启服务
systemctl reload crond //重新载入配置
systemctl status crond //查看服务状态
crontab -l //查看root用户的crontab任务
crontab -r //删除root用户所有crontab任务
crontab -u //使用者名称
3.创建示例
用户 默认只有一个配置文件
cd /var/spool #student 用户到pool目录下
crontab -e #直接编辑
# 编辑crontab文件 中间以"空格符"分开
20 14 * * * /usr/bin/date >> ~/test/cron0.cron #确定时间 cron0.cron
*/1 14-16 * * * /usr/bin/date >> ~/test/cron1.cron #间隔时间 cron1.cron;/1 每一分钟; 14-16点
*/1 14-16 * * * /usr/bin/date >> ~/test/cron1.txt #间隔时间 cron1.cron;/1 每一分钟; 14-16点
*/15 * * * * /usr/bin/date >> ~/test/cron15.txt #间隔时间 cron15.cron
0 7-23/2,8 * * * echo "Have a good dream" >> ~/test/day.txt #晚上11点到早上8点之间每两个小时,早上八点
crontab -l #查看