cpfeil
Posts: 15
Joined: Wed Dec 25, 2013 1:35 pm

crontab does not perform network check

Tue Feb 18, 2014 10:13 pm

Hi,

I have installed two crontabs:

Code: Select all

* * * * * /usr/bin/checknetwork

* * * * * /usr/bin/gpio-test2.py
The second line is a python script to control my piface. It works well and is executed every minute!

But the first line is not executed at all. If i call the checknetwork skript directly, it reconnects if wlan0 has no IP address. But the execution using crontab does not work.

Code: Select all

echo "Performing Network check for wlan0"

if ifconfig wlan0 | grep -q "inet Adresse:" ; then
    echo "Network is Okay"
else
    echo "Network connection down! Attempting reconnection."
    ifdown wlan0
    sleep 5
    ifup --force wlan0
    ifconfig wlan0 | grep "inet Adresse:"
fi

echo "Current Setting:" + ifconfig wlan0 | grep "inet Adresse:"


exit 0
Any ideas?

Christian

sander2
Posts: 60
Joined: Wed Jul 04, 2012 2:22 pm

Re: crontab does not perform network check

Wed Feb 19, 2014 5:51 am

Whose crontab is this: pi, or sudo's?

So, what is the output of:

Code: Select all

sudo crontab -l

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: crontab does not perform network check

Wed Feb 19, 2014 7:11 am

I would not execute a script every minute. I have seen cron kill a system before when a script was run every minute.


I would also add a hash bang.

Code: Select all

#!/usr/bin/python
.
.
.
.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

cpfeil
Posts: 15
Joined: Wed Dec 25, 2013 1:35 pm

Re: crontab does not perform network check

Wed Feb 19, 2014 5:28 pm

sander2 wrote:Whose crontab is this: pi, or sudo's?

So, what is the output of:

Code: Select all

sudo crontab -l
Hi,

the output is:

Code: Select all

[email protected]:/home/pi# sudo crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

5 * * * * /usr/bin/checknetwork

@reboot /usr/bin/gpio-test2.py
I changed the python script from Running every minute.

checknetwork is not executed every three minutes.

What is wrong?

Christian

User avatar
RaTTuS
Posts: 10323
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: crontab does not perform network check

Wed Feb 19, 2014 5:32 pm

5 * * * * /usr/bin/checknetwork

means 5 past the hour

*/5 * * * * /usr/bin/checknetwork
means every 5 mins
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

hk1963
Posts: 25
Joined: Fri Dec 06, 2013 6:06 pm

Re: crontab does not perform network check

Wed Feb 19, 2014 5:37 pm

So this is root's crontab.

You may also add something like

*/5 * * * * /usr/bin/checknetwork >>/var/log/checknetwork.log 2>&1

to the entry so any output of the script is written into some file you can check for messages.

Also keep in mind that when run by cron, the command has most environment variables not available, so maybe some commands in the script are not in the path and fail to get run.

cpfeil
Posts: 15
Joined: Wed Dec 25, 2013 1:35 pm

Re: crontab does not perform network check

Wed Feb 19, 2014 7:43 pm

Hi,

thank you, this was a very helpful comment!

The log says the following:

Code: Select all

/usr/bin/checknetwork: 7: /usr/bin/checknetwork: ifdown: not found
/usr/bin/checknetwork: 9: /usr/bin/checknetwork: ifup: not found
/usr/bin/checknetwork: 10: /usr/bin/checknetwork: ifconfig: not found
So it does not know the commands. Why? Any ideas?

regards,
Christian

cpfeil
Posts: 15
Joined: Wed Dec 25, 2013 1:35 pm

Re: crontab does not perform network check

Wed Feb 19, 2014 8:31 pm

Hi,

I think i fixed the problem on my own.
The full path to ifconfig etc. was missing. I added three aliases and now it seems to work.

Code: Select all

alias ifup='/sbin/ifup'
alias ifdown='/sbin/ifdown'
alias ifconfig='/sbin/ifconfig'

echo "Performing Network check for wlan0";

if ifconfig wlan0 | grep -q "inet Adresse:" ; then
    echo "Network is Okay"
else
    echo "Network connection down! Attempting reconnection."
    ifdown wlan0
    sleep 5
    ifup --force wlan0
    ifconfig wlan0 | grep "inet Adresse:"
fi

echo "Current Setting:" + ifconfig wlan0 | grep "inet Adresse:"


exit 0
Thank you all for your help!

Christian

Return to “Raspbian”