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