1

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.

gideon
  • 1,145
  • 2
  • 13
  • 28

2 Answers2

4

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.

phemmer
  • 5,909
  • 2
  • 27
  • 36
1

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.

Stefan Lasiewski
  • 23,667
  • 41
  • 132
  • 186