Page 1 of 1
Script runs differently from cron
Posted: Thu May 23, 2013 10:33 pm
by jmahoney
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
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
Re: Script runs differently from cron
Posted: Fri May 24, 2013 2:09 am
by SirLagz
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
Re: Script runs differently from cron
Posted: Fri May 24, 2013 5:29 am
by rpdom
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.
Re: Script runs differently from cron
Posted: Fri May 24, 2013 10:47 am
by jmahoney
@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
Re: Script runs differently from cron
Posted: Fri May 24, 2013 10:55 am
by sprinkmeier
I will try adding /bin/ anyway,
Not everything lives in
/bin, use
which to find things:
Re: Script runs differently from cron
Posted: Fri May 24, 2013 11:09 am
by jmahoney
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
one question remains though- why do I not need to add /sbin before ip addr show too?
Re: Script runs differently from cron
Posted: Fri May 24, 2013 11:14 am
by RaTTuS
also put temp files in /tmp that way you don't keep writing to your sdcard...

Re: Script runs differently from cron
Posted: Fri May 24, 2013 11:46 am
by rpdom
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
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.
Re: Script runs differently from cron
Posted: Fri May 24, 2013 11:55 am
by jmahoney
@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!
Re: Script runs differently from cron
Posted: Fri May 24, 2013 8:18 pm
by sprinkmeier
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 
Re: Script runs differently from cron
Posted: Mon May 27, 2013 2:24 am
by SirLagz
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
