crontab
是個管理定時任務的工具,做用是在特定時間(經過crontab的語法配置),自動執行特定任務(想讓它執行什麼,就寫個腳本或bash命令)。當你天天都須要執行腳本幹一些重複工做的時候,這個東西就派上用場了。crontab
時踩得一些坑,當我按照順序作完配置以後,卻發現crontab中的task怎麼也跑步起來,因而google了一下問題,找到了幾個相關blog,結合在一塊兒驗證,終於解決了問題。crontab 爲啥不執行呢?html
➜ ~ sudo launchctl list | grep cron 208 0 com.vix.cron 有記錄。查看一下啓動項的配置。 ➜ ~ locate com.vix.cron WARNING: The locate database (/var/db/locate.database) does not exist. To create the database, run the following command: sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist Please be aware that the database can take some time to generate; once the database has been created, this message will no longer appear.
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist // 這個指令會花費必定時間
~ cat /System/Library/LaunchDaemons/com.vix.cron.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.vix.cron</string> <key>ProgramArguments</key> <array> <string>/usr/sbin/cron</string> </array> <key>KeepAlive</key> <dict> <key>PathState</key> <dict> <key>/etc/crontab</key> <true/> </dict> </dict> <key>QueueDirectories</key> <array> <string>/usr/lib/cron/tabs</string> </array> <key>EnableTransactions</key> <true/> </dict> </plist>
<key>KeepAlive</key> <dict> <key>PathState</key> <dict> <key>/etc/crontab</key> <true/> </dict> </dict>
➜ ~ ll /etc/crontab ls: /etc/crontab: No such file or directory
➜ ~ sudo touch /etc/crontab
須要注意的是,sh腳本中的路徑,最好使用絕對路徑,不然腳本極可能將沒法正確執行linux