MacOS – Use cron to screencapture on Mountain Lion

cronmacosscreen capture

I want to set cron to capture the current desktop every hour:

screencapture -Cd -tjpg ~/Desktop/screen-`date +"%Y%m%d-%H%M"`.jpg

I tired this method but it seems doesn't work on Mountain Lion. I also tried Lingo but with no luck. Any ideas?

Best Answer

cron behaves a bit differently than a standard prompt, which is why it's not doing what you're expecting. There are two issues with the command as is:

  • Because it doesn't parse the PATH environment variable the same was as the shell, cron doesn't know where screencapture is. You need to specify the full path, /usr/sbin/screencapture.
  • As noted in the crontab(5) man page, cron treats % as a newline character:

    Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

    So you need to escape each % with a \ to get your desired result.

This crontab entry will give you a screen capture every hour, on the hour:

0 * * * * /usr/sbin/screencapture -Cd -tjpg ~/Desktop/screen-`date +"\%Y\%m\%d-\%H\%M"`.jpg

Cron Error Logging

As an aside, if you're having issues with cron, check your system mail by running the mail command in Terminal. It's not very obvious, but by default cron sends error messages there that can provide a starting point to figuring out what's going wrong.