1. 啟動cron服務
# 查看服務狀態
systemctl status crond
# 啟動
systemctl start crond
# 停止
systemctl stop crond
# 重啟
systemctl restart crond
# 重新加載
systemctl reload crond
2. 開機自動啟動cron服務
systemctl start crond.service
3. 通過crontab命令管理定時任務
crontab命令用法如下:
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-x enable debugging
4. 定時任務格式
查看/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
# [分] [時] [日] [月] [周] [需要執行的命令]
詳細的cron表達式可以查看這篇文章:
5. 添加一個定時任務
# 進入編輯模式,按照格式添加,保存退出
# crontab會自動檢查語法是否正確,如果錯誤會有提示。
[root@localhost /]# crontab -e
*/1 * * * * /home/app/zipkin/zipkin-dependencies.sh
*/1 * * * *:表示每分鐘執行一次
/home/app/zipkin/zipkin-dependencies.sh:是需要執行的命令
添加成功后,查看定時任務列表:
[root@localhost /]# crontab -l
*/1 * * * * /home/app/zipkin/zipkin-dependencies.sh
6. 查看執行日志
cat /var/log/cron