快轉到主要內容

Ubuntu "Crontab" 工作排程

·1115 字
Computer-Science Ubuntu
目錄

crontab
#

可以使用「crontab」指令控制工作排程。


Edit, List, Remove
#

1crontab -e # 編輯排程
2crontab -l # 列出清單
3crontab -r # 刪除排程

如果權限允許,那也可以在上面所述的指令下,加上-u <user>參數指定欲更動何使用者的排程。


Format
#

 1# Edit this file to introduce tasks to be run by cron.
 2# 
 3# Each task to run has to be defined through a single line
 4# indicating with different fields when the task will be run
 5# and what command to run for the task
 6# 
 7# To define the time you can provide concrete values for
 8# minute (m), hour (h), day of month (dom), month (mon),
 9# and day of week (dow) or use '*' in these fields (for 'any').
10# 
11# Notice that tasks will be started based on the cron's system
12# daemon's notion of time and timezones.
13# 
14# Output of the crontab jobs (including errors) is sent through
15# email to the user the crontab file belongs to (unless redirected).
16# 
17# For example, you can run a backup of all your user accounts
18# at 5 a.m every week with:
19# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
20# 
21# For more information see the manual pages of crontab(5) and cron(8)
22# 
23# m h  dom mon dow   command

在使用編輯參數「-e」後,會用預設編輯器編輯一個如上的暫存檔案。
檔案中預設是沒有排程的,但有一些註解。

直接在註解下方新增一行,照著格式輸入設定即可。
可以在最後一行看到其使用格式「m h dom mon dow command」,其含意如下:

FormatRangeExample
m, Minute, 分0-59每個小時的 30 分 30 * * * * Command
h, Hour, 時0-23每天的 1 時 30 分 30 1 * * * Command
dom, Day of Month, 日1-31每月的 5 號 12 時 * 12 5 * * Command
mon, Month, 月0-12每年 7 月 14 的 0 時 1 分 1 0 14 7 * Command
dow, Day of Week, 星期0-7 (0,7 皆表示星期天)每個星期一 * * * * 1 Command
command, 指令每週 2 的 23 時整執行 backup 指令 0 23 * * 2 backup

特殊值符號:

SignExplanationExample
*任意時段每天 12 時的任意分 * 12 * * * Command
,分隔多個時段每天的 9 時及 20 時 * 9,12 * * * Command
-時間範圍每天的 7 至 10 時 * 7-10 * * * Command
/n循環時段每週 2 的每個小時 * \1 * * 2

僅在開機時執行則使用「@reboot」,範例每次開機執行echo 'hello' > /tmp/hi

1@reboot echo 'hello' > /tmp/hi

Permission
#

可以透過創建White List檔案來指定哪些使用者可以創建排程;
或者透過創建Black List檔案來指定哪些使用者不能創建排程。

  • White List:/etc/crontab.allow
  • Black List:/etc/crontab.deny

程式的判定會先查看是否存在White List的檔案/etc/crontab.allow
若不存在,則會查看Black List的檔案/etc/crontab.deny

例如僅允許使用者adminadmin2執行 crontab 指令,則編輯/etc/crontab.allow

1# /etc/crontab.allow
2admin
3admin2

若僅不允許使用者guestguest2執行 crontab 指令,則先確保/etc/crontab.allow不存在,接著編輯/etc/crontab.deny

1# /etc/crontab.deny
2guest
3guest2

/etc/init.d/cron
#

輸入/etc/init.d/cron並透過status, start, stop, restart等參數
來對 Cron 服務狀態查看, 啟動, 停止, 重新啟動


References
#

《循環執行的工作排程 - dywang》https://dywang.csie.cyut.edu.tw/dywang/linuxSystem/node71.html

Alpaca
作者
Alpaca
No one can stop my feet.

相關文章

How to set timezone on ubuntu?
·183 字
Computer-Science Ubuntu
Ubuntu 更改家目錄語言
·574 字
Computer-Science Ubuntu

簡單幾步變更家目錄的語言

Ubuntu 更換 /Home 到新硬碟
·1364 字
Computer-Science Ubuntu Disk

如果發現當初掛載好的空間不夠用了,那麼擴充硬碟上去,然後照著步驟一步步的將檔案們搬家吧。

How to use PPPoE to connect network on Ubuntu?
·353 字
Computer-Science Ubuntu PPPoE

在 Ubuntu 用 PPPoE 連線中華電信固定 IP。

Raspberry Pi 4 (Ubuntu) configure auto connect to wifi
·142 字
Computer-Science Raspberry PI Wi-Fi Ubuntu

How to make ubuntu auto connect to wifi in raspberry pi 4?

Linux + Win 雙系統卻不見 Grub 開機選單?
·1691 字
Computer-Science Windows Ubuntu Grub

安裝了 Linux + Windows 的雙系統,但卻不見開機選單?放心,很多人跟你一樣。