LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   script in cron.hourly not running hourly (http://www.linuxquestions.org/questions/linux-software-2/script-in-cron-hourly-not-running-hourly-485104/)

unholy 09-19-2006 06:24 PM

script in cron.hourly not running hourly
 
I've put a file called 'test' in /etc/cron.hourly. Its code is:
Code:

touch /home/ja/hourly
I made it executable and waited for the turn of the hour, but nothing happened. The file did not appear in /home/ja.

Here is my crontab (I dont understand this file, even after reading the man pages)
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily
47 6 * * 7 root test -x /usr/sbin/anacron || run-parts --report /etc/cron.weekly
52 6 1 * * root test -x /usr/sbin/anacron || run-parts --report /etc/cron.monthly
#

matthewg42 09-19-2006 07:08 PM

Make sure your file /etc/cron.hourly/test is executable and has the shebang line (#!/bin/bash):

Code:

$ cd /etc/cron.hourly
$ ls -l
total 4
-rw-r--r-- 1 root root 35 2006-09-20 00:52 test
$ cat test
#!/bin/bash
touch /home/ja/hourly
$ sudo chmod 755 test
$ ls -l
total 4
-rwxr-xr-x 1 root root 35 2006-09-20 00:52 test

Make sure /home/ja/hourly is writable by the user which runs the crontab. While this is almost certainly the case since cron.hourly runs as root, however there are some times when root will not be able to update the file. For example the filesystem in which that file resides is not mounted read/write, or the directory in which the file resides doesn't exist (I don't think touch creates directories if they don't already exist). Try echoing something and re-directing it into a file which is surely writable. Add this to the test script:
Code:

echo "cron job running at `date`" > /tmp/testcron
According to the /etc/crontab you posted, the cron.hourly runs at 17 minutes past the hour, check after this time.

Cron [at least the one which comes with Unbuntu] works out if there have been changes to the exitsing jobs by checking the timestamp on some directories. Check that the /etc/cron* stuff doesn't have screwy timestamps.

Make sure touch is in the PATH which is specified in your /etc/crontab, or modify your test script to speciy the full path to touch. The biggest cause of problems with cron jobs is that people assume they run with the same environment variables as their shell, and this is not true - cron jobs get a very "naked" environment, which may cause some programs to not be found, or not function as expected.

That's all I can think of right now.

unholy 09-19-2006 08:21 PM

Hi Matt,

Thanks for that informative post! I understand the crontab file better now. It turns out that the problem with my file was that it didn't have the 'shebang' line :)

Thanks again for your help. There are a lot of things I want to schedule on my kubuntu machine.

Best wishes,
unholy


All times are GMT -5. The time now is 09:21 AM.