cron runs but nothing happens

6

2

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.

Joseph

Posted 2009-08-04T03:49:21.723

Reputation: 2 109

isn't this more like.. serverfault question? – codingbear – 2009-08-04T04:04:24.720

2Users use cron. – Richard Hoskins – 2009-08-04T04:08:05.413

can you provide output of crontab -l ? – John T – 2009-08-04T04:09:40.497

crontab -l outputs the same line in the original question. – Joseph – 2009-08-04T04:17:13.320

2Does /etc/cron.allow exist? – Richard Hoskins – 2009-08-04T04:38:28.037

Yes, and the only thing contained in it is "noc". – Joseph – 2009-08-04T04:57:13.660

What are the permissions of /etc/cron.allow? Does cron.deny exist? – Richard Hoskins – 2009-08-04T05:05:48.910

-rw-r--r-- 1 root root 10 2009-08-04 01:01 cron.allow

cron.deny does not exist. – Joseph – 2009-08-04T05:06:59.127

@Joseph: Fool of a took! ;-) – Pete – 2009-08-04T16:33:29.233

Answers

8

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.

David Mackintosh

Posted 2009-08-04T03:49:21.723

Reputation: 3 728

I reduced my script and nothing happens still. The noc user should be the only user running the script as it is the only account. I don't have email services installed on this server. – Joseph – 2009-08-04T04:30:50.487

Erm... does ubunto use cron.allow and cron.deny? – David Mackintosh – 2009-08-04T18:23:05.100

OK, saw your update, glad you got it figured out. – David Mackintosh – 2009-08-04T18:29:07.363

6

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?

Pete

Posted 2009-08-04T03:49:21.723

Reputation: 537

2*/5 runs it every 5 minutes. having just 5 will run it once every hour at 5 minutes past the hour. – pgs – 2009-08-04T04:37:57.180

Adding the full path did not change anything. I created the contrab with "crontab -e". The editor is nano. – Joseph – 2009-08-04T05:08:36.563

1

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

pgs

Posted 2009-08-04T03:49:21.723

Reputation: 3 153

No. Yes. Same thing. – Joseph – 2009-08-04T05:05:33.483

0

If you have any doubts about your script, put the line below at the top of the script:

 #!/bin/bash -x

(if you don't use this shell, just put the good one)

Starting up the subshell with the -x (eXplicit) option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.

DeaconFraust

Posted 2009-08-04T03:49:21.723

Reputation: 31