project: blogtarget: how-to-enable-crontab-on-osx.mddate: 2015-12-16status: publishtags: - OS X - MAC - crontab - 定时任务categories: - OS X - crontab
Background
Some things on the books are more important, I use Git to manage, so I can back it up to a git repository. But I like to lazy, do not like to always manually perform a series of backup commands, so I thought of writing a backup script, want to crontab to regular backup.
The script was written, and a crontab was added:
# m h dom mon dow command30 11,17 * * * /Users/clarence/bin/daily-backup
But why is it not implemented?
Asked the next Niang and Google, the results of many articles are about how to use OS X now comes with the Launchctl to perform scheduled tasks. However, this script is like every morning and afternoon to carry out, with Launchctl to engage in a bit of trouble. So today we have to explore how to enable crontab. The results were found in a few moments and are now shared as follows:
How to enable Crontab
First, since OS X's timing tasks are all managed by LAUNCHCTL, see if the cron task is there:
$ LaunchAgents sudo launchctl list | grep cron83968 0 com.vix.cron
Sure enough in there. Then check the configuration of this startup item:
$ launchagents Locate com.vix.cron/system/library/launchdaemons/com.vix.cron.plist$ launchagents 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> <ke y>programarguments</key> <array> <string>/usr/sbin/cron</string> </array> <key>KeepAlive</key> <dict> <key>PathState</key> <dict> & lt;key>/etc/crontab</key> <true/> </dict> </dict> <key>queuedirec tories</key> <array> <string>/usr/lib/cron/tabs</string> </array> <key> ; Enabletransactions</key> <true/></dict></pliSt>
Note that there is a keepalive condition that /etc/crontab
exists:
<key>KeepAlive</key> <dict> <key>PathState</key> <dict> <key>/etc/crontab</key> <true/> </dict> </dict>
So, let's see if it's because this /etc/crontab
doesn't exist that causes the task in cron to not work:
$ LaunchAgents ll /etc/crontabls: /etc/crontab: No such file or directory
Sure enough, this file does not exist.
Then create it!
$ sudo touch /etc/crontab
Try again if the cron task starts successfully ... Sure enough, it will start successfully!
Knock it off ~
How do I enable crontab on Mac OS x?