cron runs but nothing happens

I have created a script called "forward_email.sh" and set permissions to 777. It contains the following line:

echo "It worked: $(date)" >> /home/noc/email.log

I created a cron job with the following line:

*/5 * * * * /home/noc/forward_email.sh

The problem is that the "email.log" file never gets changed. I even set the permissions to 666. When I run the script manually, it works perfectly.

The cron job is running according to the "/var/log/cron.log" file.

Can someone point me into the right direction as to what might be going wrong? This is a minimal virtual machine install of Ubuntu Server 9.04 with cron installed via apt-get.

UPDATE: I made a very stupid mistake. I named my script "forward_mail.sh". I wasn't until I installed some email services and got the following error email from Cron that I discover my mistake.

/bin/sh: /home/noc/forward_email.sh: not found

I changed the name of the file, and it works now.


Solution 1:

Lots of potential things off the top of my head.

Are you sure the crontab entry is really pointing at your script? For example, if you want to run a script such as ~noc/bin/script.sh, but have an entry like /home/noc/bin/script (ie a subtle typo) then of course it won't work. I usually check my crontab entries by copying the command out of crontab with my mouse, then pasting it into a command prompt, just to be 100% sure that it is going to run what I want it to.

Does your script contain only the indicated line? Usually you have to include something like:

#!/bin/bash

...at the top of your script to get it to work.

If it is more complicated than the single line, are you sure the programming logic will permit it to run that particular line?

Try reducing your script to

#!/bin/bash
echo "Bing!" >> /tmp/cronjob

...to see what happens. If that works, you have a programming issue in your cron script. If it fails, you have a cron issue.

Is the 'noc' user the one running the script? If not, are you sure that the user running the cron script has read/write access through /home/noc to the email.log file?

Where is the error email from the user running the cronjob going? Is an error email being generated, and then getting sent somewhere you are not expecting or maybe getting dumped altogether? Try a cronjob of

#!/bin/bash
echo "Bing!"

...and then try to figure out where the email that is generated goes.

Are cron.deny and/or cron.allow in play? If /etc/cron.allow does exist, then the user noc must be listed in it; if /etc/cron.deny exists. the user noc must not be listed in it. On RedHat, if neither cron.allow nor cron.deny exist, then ONLY the root user will be permitted to use cron.

Solution 2:

Cron runs with limited environment variables set, especially your $PATH. Try putting the full path to any binaries. So /home/noc/forward_email.sh should be:

/bin/echo "It worked: $(date)" >> /home/noc/email.log

Also, what does */5 do at the beginning of your script? */5 runs once every five minutes, 5 runs once ’5 minutes past the hour every hour.

5    *   *   *    * /home/noc/forward_email.sh
#*   *   *   *    *  command to be executed
#-   -   -   -    -
#|   |   |   |    |
#|   |   |   |    +----- day of week (0 - 6) (Sunday=0)
#|   |   |   +------- month (1 - 12)
#|   |   +--------- day of month (1 - 31)
#|   +----------- hour (0 - 23)
#+------------- min (0 - 59)

Finally, did you edit & save the crontab with the following command:

crontab -e

... where your editor is chosen by the environment variable $EDITOR?

Solution 3:

echo "It worked: $(date)" >> /home/noc/email.log

Does /home/noc/email.log exist before you run the script? Is it writable by the user running the cron job? What happens if you create it beforehand? e.g.

touch /home/noc/email.log
chmod 666 /home/noc/email.log