天天看點

Mac中的定時任務利器:launchctl

launchctl是一個統一的服務管理架構,可以啟動、停止和管理守護程序、應用程式、程序和腳本等。

launchctl是通過配置檔案來指定執行周期和任務的。

當然mac也可以像linux系統一樣,使用crontab指令來添加定時任務,這裡就不贅述,具體可參見:

OS X 添加定時任務

下面将手把手教你在mac上建立定時任務。(任務目标:每天晚上十點定時執行/Users/demo/helloworld.py的python程式)

1. 建立run.sh腳本

進入

helloworld.py

程式所在目錄

cd /User/demo

建立run.sh腳本

vi run.sh

添加執行

helloworld.py

的指令

#!/bin/sh

# 記錄一下開始時間
echo `date` >> /Users/demo/log &&
# 進入helloworld.py程式所在目錄
cd /Users/demo &&
# 執行python腳本(注意前面要指定python運作環境/usr/bin/python,根據自己的情況改變)
/usr/bin/python helloworld.py
# 運作完成
echo 'finish' >> /Users/demo/log
           

:wq

儲存退出

注意,腳本要改成可執行的權限

chmod 777 run.sh

2. 編寫plist檔案

launchctl 将根據plist檔案的資訊來啟動任務。

plist腳本一般存放在以下目錄:

  • /Library/LaunchDaemons

    -->隻要系統啟動了,哪怕使用者不登陸系統也會被執行
  • /Library/LaunchAgents

    -->當使用者登陸系統後才會被執行

更多的plist存放目錄:

~/Library/LaunchAgents 由使用者自己定義的任務項

/Library/LaunchAgents 由管理者為使用者定義的任務項

/Library/LaunchDaemons 由管理者定義的守護程序任務項

/System/Library/LaunchAgents 由Mac OS X為使用者定義的任務項

/System/Library/LaunchDaemons 由Mac OS X定義的守護程序任務項

~/Library/LaunchAgents

,建立一個plist檔案

com.demo.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <!-- Label唯一的辨別 -->
  <key>Label</key>
  <string>com.demo.plist</string>
  <!-- 指定要運作的腳本 -->
  <key>ProgramArguments</key>
  <array>
    <string>/Users/demo/run.sh</string>
  </array>
  <!-- 指定要運作的時間 -->
  <key>StartCalendarInterval</key>
  <dict>
        <key>Minute</key>
        <integer>00</integer>
        <key>Hour</key>
        <integer>22</integer>
  </dict>
<!-- 标準輸出檔案 -->
<key>StandardOutPath</key>
<string>/Users/demo/run.log</string>
<!-- 标準錯誤輸出檔案,錯誤日志 -->
<key>StandardErrorPath</key>
<string>/Users/demo/run.err</string>
</dict>
</plist>
           

3. 加載指令

launchctl load -w com.demo.plist

這樣任務就加載成功了。

更多的指令:

# 加載任務, -w選項會将plist檔案中無效的key覆寫掉,建議加上
$ launchctl load -w com.demo.plist

# 删除任務
$ launchctl unload -w com.demo.plist

# 檢視任務清單, 使用 grep '任務部分名字' 過濾
$ launchctl list | grep 'com.demo'

# 開始任務
$ launchctl start  com.demo.plist

# 結束任務
$ launchctl stop   com.demo.plist
           

如果任務呗修改了,那麼必須先unload,然後重新load

start可以測試任務,這個是立即執行,不管時間到了沒有

執行start和unload前,任務必須先load過,否則報錯

stop可以停止任務

番外篇

plist支援兩種方式配置執行時間:
  • StartInterval: 指定腳本每間隔多長時間(機關:秒)執行一次;
  • StartCalendarInterval: 可以指定腳本在多少分鐘、小時、天、星期幾、月時間上執行,類似如crontab的中的設定,包含下面的 key:
Minute <integer>
The minute on which this job will be run.
Hour <integer>
The hour on which this job will be run.
Day <integer>
The day on which this job will be run.
Weekday <integer>
The weekday on which this job will be run (0 and 7 are Sunday).
Month <integer>
The month on which this job will be run.
           
plist部分參數說明:
  1. Label:對應的需要保證全局唯一性;
  2. Program:要運作的程式;
  3. ProgramArguments:指令語句
  4. StartCalendarInterval:運作的時間,單個時間點使用dict,多個時間點使用 array <dict>
  5. StartInterval:時間間隔,與StartCalendarInterval使用其一,機關為秒
  6. StandardInPath、StandardOutPath、StandardErrorPath:标準的輸入輸出錯誤檔案,這裡建議不要使用 .log 作為字尾,會打不開裡面的資訊。
  7. 定時啟動任務時,如果涉及到網絡,但是電腦處于睡眠狀态,是執行不了的,這個時候,可以定時的啟動螢幕就好了。
更多的參數參見: mac官方文檔

參考:

Mac執行定時任務之Launchctl