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