How can I tell if my hourly cron job has run?

Solution 1:

You should look in your /var/log/syslog log file. If a cron has run, it would have a line like:

Jun 11 19:09:01 penguin CRON[17376]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete)
Jun 11 19:17:01 penguin CRON[17799]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

For troubleshooting tips, see https://help.ubuntu.com/community/CronHowto#Troubleshooting_and_Common_Problems

Solution 2:

One major pitfall to cron is cron runs in an extremely limited shell environment, as a result a lot of variables aren't exported in to the environment, mainly $PATH. Make sure you use all absolute paths to executable, including common functions like echo, uptime, date, etc all need to use full paths (/bin/echo, /bin/date, /usr/bin/uptime). To determine the path to an executable, you can use the which command like so: which echo - this will show you the full path to that tool.

Solution 3:

Try changing the first line of your script (the interpreter) to:

#!/bin/bash

I've also had problems in the past, with environment variables and PATH issues. After changing the interpreter to bash my issues were gone.

Solution 4:

Given I've added the clearme.sh script in /etc/cron.hourly/

Just filter CRON tasks in terminal with the powerful egrep and awk:

$ cat /var/log/syslog | egrep clearme | awk "{ print $1 }" > ~/Desktop/cronlog.txt

The output will look like:

Jan 14 15:20:01 markets-dev CRON[10089]: (dminca) CMD (root /etc/cron.hourly/clearme.sh)
Jan 14 15:40:01 markets-dev CRON[18042]: (dminca) CMD (root /etc/cron.hourly/clearme.sh)
Jan 14 16:00:01 markets-dev CRON[22817]: (dminca) CMD (root /etc/cron.hourly/clearme.sh)
Jan 14 16:20:01 markets-dev CRON[28183]: (dminca) CMD (root /etc/cron.hourly/clearme.sh)
Jan 14 16:40:01 markets-dev CRON[411]: (dminca) CMD (root /etc/cron.hourly/clearme.sh)
Jan 14 17:00:01 markets-dev CRON[5442]: (dminca) CMD (root /etc/cron.hourly/clearme.sh)
Jan 14 17:20:01 markets-dev CRON[11935]: (dminca) CMD (root /etc/cron.hourly/clearme.sh)

To explain everything step-by-step:

  1. cat /var/log/syslog - print me the System log
  2. egrep clearme - but only select rows that contain the text clearme
  3. awk "{ print $1 }" - print me that row that contains the text clearme
  4. > ~/Desktop/cronlog.txt - output the results in the file cronlog.txt located on the Desktop directory.

The 4th step is optional. It will just print results in the terminal instead of the file.