What's the difference between `cron` and `crontab`

147 阅读1分钟

Definitions

cron is a daemon to execute scheduled commands which in the crontab files.

A crontab(5) file contains instructions to the cron(8) daemon of the general form: run this command at this time on this date. Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab.

The cron utility searches /usr/lib/cron/tabs which is the directory for personal crontab files and /etc/crontab(if exist), the details can see in /System/Library/LaunchDaemons/com.vix.cron.plist as below:

<?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>

Cron file examples

  # use /bin/sh to run commands, overriding the default set by cron
  SHELL=/bin/sh
  # mail any output to `paul', no matter whose crontab this is
  MAILTO=paul
  #
  # run five minutes after midnight, every day
  5 0 * * *	     $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
  # run at 2:15pm on the first of every month -- output mailed to paul
  15 14 1 * *     $HOME/bin/monthly
  # run at 10 pm on weekdays, annoy Joe
  0 22 * * 1-5    mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
  23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
  5 4 * * sun     echo "run at 5 after 4 every sunday"

More examples, tips, other advance stuffs