# Mac中的定時任務利器:launchctl
>launchctl是一個統一的服務管理框架,可以啟動、停止和管理守護進程、應用程序、進程和腳本等。
>launchctl是通過配置文件來指定執行周期和任務的。
當然mac也可以像linux系統一樣,使用crontab命令來添加定時任務,這里就不贅述,具體可參見:[OS X 添加定時任務](https://link.jianshu.com/?t=http://codingpub.github.io/2016/10/27/OS-X-%E6%B7%BB%E5%8A%A0%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/)
下面將手把手教你在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](https://link.jianshu.com/?t=http://blog.csdn.net/u012390519/article/details/74542042)