Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a file called /scripts/checkInternet and it contains:

#!/bin/bash
WGET="/usr/bin/wget"

rm /tmp/index.google
$WGET -q --tries=10 
           --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
if [ ! -s /tmp/index.google ];then
        echo "Nope"
        ifdown eth0.4
        ifup eth0.4
        iptables-restore < /etc/iptables.rules
else
        echo "Connected"
fi
rm /tmp/index.google

I did chmod +x 755 this file. I can run it as root, but adding it to a cron as root it doesn't function.

What went wrong? The cron itself is being run. I see CMD (/scripts/checkInternet) in my system logs but the desired outcome is missing.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

99% of all issues running things from cron are caused because of invalid $PATH. I'm betting ifdown, ifup, and iptables-restore are all missing from the default $PATH. You need to specify the absolute path to these.

share|improve this answer
    
Patrick, you rock. –  Matt Mar 14 '12 at 4:24
add comment

The cron itself is being run. I see CMD(/scripts/checkInternet) in my system logs but the desired outcome is missing.

Think about that for a moment. If your script generates output or errors, then where would you expect the output to go?

On most Linux/Unix systems, if your script produces any output (e.g. Any output to STDOUT, or any errors to STDERR) then it will be sent via email to the owner of the crontab. In most cases, this is 'root'.

Debian's man page for cron says:

cron will look at MAILTO if it has any reason to send mail as a result of running commands in "this" crontab. If MAILTO is defined (and non-empty), mail is sent to the user so named. If MAILTO is defined but empty (MAILTO=""), no mail will be sent. Otherwise mail is sent to the owner of the crontab.

So, check the mail for the user who own's this crontab, or for the user specified by MAILTO. That email should contain the error that you are looking for.

When testing, I personally prefer to send any error logs or errors to a file, like this:

   5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1

Or I send it to syslog using logger, like this:

   5 0 * * *       $HOME/bin/daily.job 2>&1 | logger

And then run tail -f logfile in a second window.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.