Linux 設定工作排程(Crontab)

法蘭克的 iOS 世界
3 min readJan 23, 2017

--

法蘭克今天要來說明如何在 linux 上設定排程,並舉幾個例子跟大家分享。這邊舉的例子是透過 WinSCP 工具連線至主機 (linux) 的方式,並使用 Notepad++ 編輯排程設定檔,並不會直接下達 linux 的指令和使用到 vi 編輯器。在開始設定之前必須先了解要在哪設定排程和相關的指令為何。

/etc/cron.d/ 這個路徑下的所有檔案都是設定排程所用,並依檔名來區別其功能性。例如法蘭克今天要設定 TOS 的排程,則會是要在 tos_test 裡設定。

再來要說明設定排程及相關的指令,打開 tos_test 一邊看並讓法蘭克來逐一說明其代表的意義。

#是備註用的,畢竟這不是要給自己看的,所以還是寫上的好。除了# 不要看以外,每一行都代表一個工作排程。每一行都有六個欄位,這六個欄位代表的意義如下:

除了數字以外,還有一些特殊的符號可以靈活運用,如下:

設定排程指令的最後一欄,也就是第六欄,root sh {sh path},sh path 要指向執行排程的 .sh(shell script) 檔,這邊最好填上絕對路徑以避免找不到檔案的情況發生。以上都清楚明白後,就舉兩個例子來練練功了!!!

▼設定每30分鐘執行TOS例性行工程排程

打開 /etc/cron.d/tos_test 並加入

*/30 * * * * root sh /opt/TOS/UPLOAD_CSV_TO_SAP/MainJob/MainJob_run.sh

▼設定2/1下午3:00執行TOS例性行工程排程

打開 /etc/cron.d/tos_test 並加入

0 15 1 2 * root sh /opt/TOS/UPLOAD_CSV_TO_SAP/MainJob/MainJob_run.sh

▼查看例性行工程排程是否有執行

查看 /var/log/cron

▼直接下指令執行.sh

請先確認該 .sh 有沒有手動執行的權限

將目錄切換至 .sh 當前的目錄下並鍵入 ./{.sh name}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response