7

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.

9
  • isn't this more like.. serverfault question?
    – codingbear
    Aug 4, 2009 at 4:04
  • 2
    Users use cron. Aug 4, 2009 at 4:08
  • can you provide output of crontab -l ?
    – user1931
    Aug 4, 2009 at 4:09
  • crontab -l outputs the same line in the original question.
    – Joseph
    Aug 4, 2009 at 4:17
  • 2
    Does /etc/cron.allow exist? Aug 4, 2009 at 4:38

4 Answers 4

9

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.

3
  • 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
    Aug 4, 2009 at 4:30
  • Erm... does ubunto use cron.allow and cron.deny? Aug 4, 2009 at 18:23
  • OK, saw your update, glad you got it figured out. Aug 4, 2009 at 18:29
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?

2
  • 2
    */5 runs it every 5 minutes. having just 5 will run it once every hour at 5 minutes past the hour.
    – pgs
    Aug 4, 2009 at 4:37
  • Adding the full path did not change anything. I created the contrab with "crontab -e". The editor is nano.
    – Joseph
    Aug 4, 2009 at 5:08
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
1
  • No. Yes. Same thing.
    – Joseph
    Aug 4, 2009 at 5:05
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .