1

I'm logged in as user pi via SSH and have a wifi checking script wifi_test_1.sh in the following directry: /usr/src/scripts/wifi_test_1.sh

When i enter the command /usr/src/scripts/wifi_test_1.sh into the terminal, it runs; i can see the "echos", but when i enter this: */1 * * * * /usr/src/scripts/wifi_test_1.sh into my crontab -e, I can not see it running.

When i do a grep CRON /var/log/syslog I see:

Feb 24 22:35:01 raspberrypi /USR/SBIN/CRON[7537]: (pi) CMD (/usr/src/scripts/wifi_test_1.sh)

But no echos from the script.

Is the script running in the background, so i cannot see the echos from the script, when logged in as pi at terminal via SSH?

script for completeness:

#!/bin/bash

# Which Interface do you want to check/fix
wlan='wlan0'

# perform check 
if ifconfig $wlan | grep -q "inet addr:" ; then

    echo "==============="
    echo "Network is Okay"
    echo "==============="

else

    echo "================================================="
    echo "Network connection down! Attempting reconnection."
    echo "================================================="


    ifdown $wlan
    sleep 10
    ifup --force $wlan
    ifconfig $wlan | grep "inet addr"
fi
exit 0
2

When working with CRON I always output the result to a dedicated file, such as

*/1 * * * *  /usr/src/scripts/wifi_test_1.sh >> /tmp/cronoutput.log

rather than try and setup a mail daemon

2

There are a couple of things I'd do differently and it's a combination of a fuller version of Octopus' comment, a modified version of the answer by rob and my own thing.

By default, CRON's output is send to the Mail Transport Agent or MTA and on Debian the default is exim4 and you can read the mail messages with mutt for example. And with your crontab entry it would send both stdout (your echo statements) and stderr (errors) to it.

If you don't have and/or don't want an MTA, rob's variant works good too, but you'll miss any errors that would occur (see my next point for that). You can have both stdout and stderr if you change it to this:
*/1 * * * * /usr/src/scripts/wifi_test_1.sh >> /tmp/cronoutput.log 2>&1
This will send stderr (fd 2) to stdout (fd 1) and therefor ends up in your custom log file. Note that /tmp/ is cleared on reboot and you'd thus lose any files stored there, but in this case that may not be an issue.

The last suggestion I have is to use FULL paths in your commands.
While developing I'd add echo "PATH: " $PATH so that your $PATH is send to stdout and that makes it easy to spot errors or wrong assumptions in your code. ifconfig, ifup and ifdown are in /sbin/ and that was not in the $PATH of the 'cron'-user. Adding a export PATH=/sbin/:$PATH at the beginning of the script fixed that.
But I actually prefer to just use the full paths in my code and would thus have used /sbin/ifconfig, /sbin/ifup and /sbin/ifdown.

  • 1
    upvote to help ward off negative energy. – Steve Robillard Dec 10 '15 at 14:41
  • 1
    :-) ... and with that I've now earned the privilege to downvote LOL – Diederik de Haas Dec 10 '15 at 14:50
  • that is karma baby – Steve Robillard Dec 10 '15 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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