Mac上定時運行腳本工具--launchctl


在Mac上可以像在Linux上一樣,使用crontab來定時運行腳本,但蘋果並不推薦這個方法。蘋果推薦使用Launchctl來完成定時任務。

 

首先,我們先寫一個可執行的腳本,列子為php腳本,名字為test.php.

 

其次,要到相對應的目錄下面建立plist文件。

一共有5個文件夾,差別在於,Agents文件夾下的plist是需要用戶登錄后,才會加載的,而Daemons文件夾下得plist是只要開機,可以不用登錄就會被加載

 

然后,我們編寫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>
    <key>Disabled</key>
    <false/>
    <key>KeepAlive</key>
    <false/>
    <key>Label</key>
    <string>com.ryan.test</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/php</string>
        <string>/Users/Ryan/Zend/workspaces/DefaultWorkspace/test/test.php</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StandardErrorPath</key>
    <string>/Users/Ryan/Documents/test_log/stderr</string>
    <key>StandardOutPath</key>
    <string>/Users/Ryan/Documents/test_log/stdout</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>15</integer>
        <key>Minute</key>
        <integer>5</integer>
    </dict>
</dict>
</plist>

 

最后,我們需要load寫好的plist文件

launchctl load -w /Library/Daemons/com.ryan.test.plist    // 加載

launchctl unload -w /Library/Daemons/com.ryan.test.plist    // 卸載

 

參考文檔地址:http://launchd.info

工具地址:http://www.soma-zone.com/LaunchControl/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM