Files
Obsidian-Main/00. Inbox/Linux/crontab.md

60 lines
2.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
### 加入
使用`crontab -e`,然後加入這一行:
`*/1 * * * * /home/awin/script/ddns.sh`
### 說明
![[Pasted image 20240111231507.png]]
依序是 **分鐘,   小時,   日期,   月份,    星期,   command**
參數為 0-59,   0-23,   1-31,  1-21,   0-6,   需要執行的command
**※ 星期參數為 0 代表星期日**
- 【*】:星號,代表任何時刻都接受的意思
- 【,】逗號代表分隔時段。例如30 9,17 * * * command代表早上 9 點半和下午五點半都執行 command。
- 【-】減號代表一段時間範圍。例如15 9-12 * * * command代表從 9 點到 12 點的每個 15 分都執行 command。
- 【/n】斜線n 代表數字,表示每個 n 單位間隔。例如:*/5 * * * * command代表每隔 5 分鐘執行一次 command。
還有一些人性化的參數,一次取代全部五個數字參數
-@reboot】 :僅在開機的時候執行一次。
-@yearly 一年執行一次和0 0 1 1 * command效果一樣。
-@annually】:(和@yearly一樣
-@monthly一個月執行一次和0 0 1 * * command效果一樣。
-@weekly一個星期執行一次和0 0 * * 0 command效果一樣。
-@daily每天執行和0 0 * * * command效果一樣。
-@midnight】:(和@daily一樣
-@hourly 每小時執行和0 * * * * command效果一樣。
### 範例
- 每 5 分鐘執行一次:
- `*/5 * * * *  root    /usr/libexec/atrun`
- 每 5 小時執行一次
- `* */5 * * * root    /usr/libexec/atrun`
- 每天 AM 5:00 執行指令(星號與星號之間要有空隔)
- `00 05   * * *  username /bin/bash /路徑/command`
- 1 至 20 號每天執行一次
- `0 1 1-20  * * root    /usr/libexec/atrun`
- 當分針移到第 5 分時,執行此 cron
- `5 * * * * root    /usr/libexec/atrun`
- 當時針移到 1 點 1 分時,執行此 cron
- `1 1 * * * root    /usr/libexec/atrun`
- 每週一的 1 點 1 分,執行
- `1 1 * * 1 root    /usr/libexec/atrun`
- 2 月 29 日時針到 1 點 1 分,執行
- `1 1 29 2 * root    /usr/libexec/atrun`
- 8 點到 16 點每 5 分鐘執行一次 cron
- `/5 8-16 * * *   root    /usr/libexec/atrun`
### crontab 命令
#### 啟動
`sudo service cron start`
#### 重新啟動
`/etc/init.d/cron restart`
#### 查詢service狀態
`sudo /etc/init.d/cron status`
#### 編輯 crontab
`crontab -e`