jmahoney
Posts: 9
Joined: Tue May 07, 2013 5:34 pm

Script runs differently from cron

Thu May 23, 2013 10:33 pm

Hi

I have a curious problem, can't seem to understand why it happens.

I have a simple script which emails me basic network interface information, I use cron to schedule it hourly.

Here is the crontab entry

Code: Select all

1 * * * * sh -x ~/hourpeep.sh
here is the script

Code: Select all

#!/bin/bash

rm ipcheck.txt
rm iwcheck.txt
rm hourpeep.txt
wget -qO- ipecho.net/plain > ipcheck.txt
echo "  [i]hostname[/i]  " >> ipcheck.txt
ip addr show >> ipcheck.txt
iwcheck wlan0 > iwcheck.txt
cat ipcheck.txt iwcheck.txt > hourpeep.txt
cat hourpeep.txt | msmtp -a default [i]myemail address[/i]

When I run from command line it works fine but when run as a cron job I do not get the iwconfig info - the file iwcheck.txt is empty.

Can someone please explain what is going on here?

Thanks

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: Script runs differently from cron

Fri May 24, 2013 2:09 am

cron doesn't set the path.
You'll need to use the full path when running commands from a script run by cron.
e.g /bin/iwconfig rather than just iwconfig
you can use the command 'which' to find the full path
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Script runs differently from cron

Fri May 24, 2013 5:29 am

Actually, cron does set the path.

What it doesn't do is read .bashrc .profile or any of those files, so any changes to the path set up in there won't take effect - such as including ~/bin

Where is the command "iwcheck"? I can't find any reference to it. If that is a script you have written yourself and put in ~/bin, then that would explain the cron job not being able to find it.

Normally you would get a mail from cron about any error, but I don't think even a local mail client is set up by default in Raspbian. You could try adding " 2> ~/cron.err" on the end of the crontab line, which would list any error messages to that file.

jmahoney
Posts: 9
Joined: Tue May 07, 2013 5:34 pm

Re: Script runs differently from cron

Fri May 24, 2013 10:47 am

@rpdom - yes there is no command iwcheck, sorry that's a typo but only in this post , the script has iwconfig > iwcheck.txt
SirLagz wrote:cron doesn't set the path.
You'll need to use the full path when running commands from a script run by cron.
e.g /bin/iwconfig rather than just iwconfig
If this is the case why does it not also have the same trouble with ip addr show?
I will try adding /bin/ anyway, also added 2>~/con.err, and let you know what happens.

Many thanks
J

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Script runs differently from cron

Fri May 24, 2013 10:55 am

I will try adding /bin/ anyway,
Not everything lives in /bin, use which to find things:

Code: Select all

$ which python
/usr/bin/python

jmahoney
Posts: 9
Joined: Tue May 07, 2013 5:34 pm

Re: Script runs differently from cron

Fri May 24, 2013 11:09 am

Ok, it works now. I used which and changed the relevant line in the script to /sbin/iwconfig wlan0 > iwcheck.txt

Thanks for all your contributions :D

one question remains though- why do I not need to add /sbin before ip addr show too?

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Script runs differently from cron

Fri May 24, 2013 11:14 am

also put temp files in /tmp that way you don't keep writing to your sdcard... ;)
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

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Script runs differently from cron

Fri May 24, 2013 11:46 am

jmahoney wrote:Ok, it works now. I used which and changed the relevant line in the script to /sbin/iwconfig wlan0 > iwcheck.txt

Thanks for all your contributions :D

one question remains though- why do I not need to add /sbin before ip addr show too?
The default path for a non-root user is /bin and /usr/bin, ip lives in /bin and that is in your path.

iwconfig is in /sbin, because it can change system settings. The root user's path includes /sbin and /usr/sbin, but as yours doesn't you need to specify it.

jmahoney
Posts: 9
Joined: Tue May 07, 2013 5:34 pm

Re: Script runs differently from cron

Fri May 24, 2013 11:55 am

@RaTTuS- i'm not sure I understand, I thought everything was on the sd card...where are /tmp files stored then?


@rpdom- thanks, that makes sense to me now!

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Script runs differently from cron

Fri May 24, 2013 8:18 pm

RaTTuS wrote:also put temp files in /tmp that way you don't keep writing to your sdcard... ;)
on my Pi (raspian) /tmp is on the SD card. I usually use /dev/shm

Code: Select all

$ df /tmp/ /dev/shm/
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root       15253388 6157948   8320608  43% /
tmpfs              99500       4     99496   1% /run/shm
$ ls -l /dev/shm
lrwxrwxrwx 1 root root 8 May 24 06:17 /dev/shm -> /run/shm
(OK.. so it turns out I'm using /run/shm :-)

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: Script runs differently from cron

Mon May 27, 2013 2:24 am

jmahoney wrote:@rpdom - yes there is no command iwcheck, sorry that's a typo but only in this post , the script has iwconfig > iwcheck.txt
SirLagz wrote:cron doesn't set the path.
You'll need to use the full path when running commands from a script run by cron.
e.g /bin/iwconfig rather than just iwconfig
If this is the case why does it not also have the same trouble with ip addr show?
I will try adding /bin/ anyway, also added 2>~/con.err, and let you know what happens.

Many thanks
J
Sorry. misworded. Other guy explained it much better :oops:
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

Return to “General programming discussion”