1. 程式人生 > >linux循環定時任務

linux循環定時任務

part owin tar 覆蓋 tab 星期 centos 1-1 jobs

crond定時任務


centos

# 重啟服務
service crond restart
-----------------------------------------
chkconfig crond on
systemctl list-unit-files crond on
# 查詢,提示被覆蓋
chkconfig --list | grep cron
------------------------------------------
systemctl list-unit-files | grep cron
crond.service               enabled 
------------------------------------------

vi /etc/cron.
cron.d/       cron.daily/   cron.deny     cron.hourly/  cron.monthly/ cron.weekly/

crontab 【選項】
-e: 編輯crontab 定時任務
-l: 查詢crontab 任務
-r:刪除當前用戶所有crontab任務

註意: crontab 是當前用戶的,只有當前用戶的權限,crontab 會綁定 user 的身份 

crontab -e
會進入一個當前用戶的文件,在這個文件下編寫任務

編寫任務格式

"""
* * * * * 執行任務
第一個* 一小時當中的第幾分鐘0-59
第二個* 一天當中的第幾個小時 0-23
第三個* 一月當中的第幾天 1-13
第四個* 一年當中的第幾個月 1-12
第五個* 一周當中的第幾個星期 0-7 0,7都代表周日
"""


例如
***********************************
10 * * * *  /root/nginx_start.sh  
會在1點10分,2點10,一小時執行一次      
************************************

************************************
如果想每隔10分鐘執行一次
*/10 * * * * * /pwd/腳本
*代表任意時間,每隔十分鐘執行一次
************************************

+++++++++++++++++++++++++++++++++++++++
 * * * * * 這個是每分鐘執行一次
 ,代表不連續時間  0 8,12,16 * * *
 每天8點整 12點整 16點整 都執行一次
+++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++
-代表連續的時間
0 5 * * 1-6
周1到周6的早上5點整執行
+++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++
45 22 * * * 每天22點45分
0 17 * *  1 每周1的17點整
0 5 1,15 * * 每月1號和5號 的 5點整
40 4 * * 1-5 周1到周5的4點40分
*/10 4 * * * 4點 4點10分 4點20分 ...4點50分 1小時執行6次
0 0 1,5 * 1 每周1 或1號 或 5號 的淩晨 
+++++++++++++++++++++++++++++++++++++++


"""
註意事項 

星期和幾號 不要同時出現,自己容易混亂

最小時間範圍是分鐘
最大時間範圍是月
超出範圍都不能實現

六個選項不能為空

腳本執行盡量使用絕對路徑
系統的path環境變量,和我們用戶的環境變量並不完全一致
""‘

系統的 crontab,使用配置文件編寫定時任務


crontab -e 是把定時任務綁定到當前用戶上的

# 使用配置文件編寫定時任務
/etc/crontab 這個是配置文件

vi /etc/crontab
####################################
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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


user-name                指定執行用戶名 
command to be executed   執行的命令或者腳本




********************************************************
第二種方法
將腳本放入
/etc/cron.{daily,weekly,monthly}
目錄下,會自動每天 每周 每月 執行一次
********************************************************

"""
定時任務 盡量把時間錯開
"""

anacron定時任務


這個任務的好處是
如果我定在5點執行一個任務
但是我4點50-5點10分關機了

當我5點10分開機後,anacron 會檢測有沒有漏掉的定時任務,會重新執行
它只會檢測目錄下的執行任務 ,crontab -e 的不會檢測

anacron 檢測周期
vi /var/spool/anacron/ 目錄記錄的是上一次執行時間
下一次執行 會和上一次進行比較

anacron 檢測時間比較粗略


vi /etc/anacrontab

“”“
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45 # 隨機延遲時間,錯峰執行
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly
”“”

來源:https://blog.csdn.net/sunt2018/article/details/86499396

linux循環定時任務