Dcatfly 2019-06-30
crontab是个管理定时任务的工具,作用是在特定时间(通过crontab的语法配置),自动执行特定任务(想让它执行什么,就写个脚本或bash命令)。当你每天都需要执行脚本干一些重复工作的时候,这个东西就派上用场了。crontab时踩得一些坑,当我按照顺序做完配置之后,却发现crontab中的task怎么也跑步起来,于是google了一下问题,找到了几个相关blog,结合在一起验证,终于解决了问题。crontab 为啥不执行呢?
➜ ~ 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脚本中的路径,最好使用绝对路径,否则脚本很可能将无法正确执行