29

I have created a file called ntpdate in /etc/cron.hourly

#!/bin/sh
/usr/sbin/ntpdate-debian
date > /tmp/william_tmp
date > /william_tmp
date > ~/william_tmp
echo test

I also did Chmod 755 to this file.

However, I can't tell if the file has run or not!

The file is not created in any of the 3 directories.

If I manually run cd / && run-parts --report /etc/cron.hourly then the files are created and I get the echo.

Can anyone recommend (ideally step by step!) instructions to test that it is working?

26

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.

2
  • As discussed in chat, thanks for this and I really hope it will help someone else... Whilst I did the fall path to ntpdate, I am so used to Windows and echo/date being just "built in", I just didn't think I would need full path... learning a lot more about Linux and it makes perfect sense!
    – wilhil
    Jun 11 '12 at 23:56
  • Does this mean that any envirnment variable included in .bash_profile does not get carried over to the job being executed using cron?
    – Sparker0i
    Jan 23 '20 at 4:24
42

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

3
  • There isn't anything in Syslog and I can't see any reason why it would fail from that link :(
    – wilhil
    Jun 11 '12 at 22:15
  • 1
    If there are no entries in syslog (grep -i cron /var/log/syslog /var/log/syslog.1), start checking whether the cron daemon is running: ps uww -C cron should contain a line with a process.
    – Lekensteyn
    Jun 11 '12 at 22:20
  • Sorry for being misleading, there are entires for Cron in the syslog, just nothing relating to this command... I know some jobs get run as every hour, I get a new mail alert... AFAIK, the ones that are working are from crontab, there is nothing in cron.hourly which is what I am trying to get working
    – wilhil
    Jun 11 '12 at 22:25
2

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.