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?
You should look in your
/var/log/syslog
log file. If a cron has run, it would have a line like:For troubleshooting tips, see https://help.ubuntu.com/community/CronHowto#Troubleshooting_and_Common_Problems
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 thewhich
command like so:which echo
- this will show you the full path to that tool.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.Given I've added the
clearme.sh
script in/etc/cron.hourly/
Just filter CRON tasks in terminal with the powerful egrep and awk:
The output will look like:
To explain everything step-by-step:
The 4th step is optional. It will just print results in the terminal instead of the file.