crontab的一些基本命令
systemctl start crond.service 启动服务
systemctl stop crond.service 停止服务
systemctl restart crond.service 重启服务
systemctl reload crond.service 重载配置文件
systemctl status crond.service 查看状态
crontab -l 显示crontab文件(显示已设置的定时任务)
crontab -e 编辑crontab文件(编辑定时任务)
crontab -r 删除crontab文件(删除定时任务)
crontab -ir 删除crontab文件提醒用户(删除定时任务)
示例
1、编写脚本文件hello.sh
$ cd /root
$ touch hello.sh
$ vi hello.sh
输入需要执行的内容
#!/bin/bash
DATE=$(date +%Y-%m-%d' '%H:%M:%S)
echo 'hello world! '$DATE >> /root/hello.log
给予hello.sh文件执行权限
$ chmod u+x hello.sh
2、编辑crontab文件
$ crontab -e
输入内容
*/1 * * * * /root/hello.sh
*/1 * * * * 表示每分钟执行一次,crontab只能创建分钟级别的
3、重载crontab配置文件
$ systemctl reload crond.service
4、查看定时任务执行结果(一分钟之后监控hello.log文件)
$ tail -f /root/hello.log
预期结果:
$ tail -f /root/hello.log
hello world! 2019-12-25 14:05:01
hello world! 2019-12-25 14:06:01
hello world! 2019-12-25 14:07:01
hello world! 2019-12-25 14:08:01
hello world! 2019-12-25 14:09:01