#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    44
    Rep Power
    10

    Script behaves different in cron than from terminal


    I have the following bash script...

    Code:
    #!/bin/bash
    
    #Set filename for links
    FILE=/home/andrew/scripts/links.txt
    
    #pkill chrome
    pkill firefox
    
    #Create an infinite loop
    while :
    do
    
    	#CHECK IP
    	pubIp=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
    	echo "Your public ip is: $pubIp..."
    	pubIpStrip=`echo $pubIp | sed 's/\.[0-9]*$/./' | sed 's/\.[0-9]*$//' | sed 's/\.[0-9]*$/./'`
    
    	compare=`grep -l $pubIpStrip ipAddress.txt`
    	if [ $compare ]; then
    		echo "Your public ip address, $pubIp, is in a bad range.  Do you have your vpn connected?"
    		echo "Now exiting..."
            	exit
    	else	
    		echo "Public ip address, $pubIp, looks good, continuing..."
    		SLEEP=$[ ( $RANDOM % ( $[ 420 - 180 ] + 1 ) ) + 180 ]
    		RAND=`cat /proc/sys/kernel/random/uuid | cut -c1-4 | od -d | head -1 | cut -d' ' -f2`
    		LINES=`cat $FILE | wc -l`
    		LINE=`expr $RAND % $LINES + 1`
    		URL=`head -$LINE $FILE | tail -1`
    		echo "Loading the following page.... $URL"
    		echo "Sleeping for $SLEEP seconds..."
    
    #		/opt/google/chrome/chrome $URL &
    		/usr/bin/firefox $URL &	
    
    		sleep $SLEEP
    #		pkill chrome
    		pkill firefox
    		SLEEP=$[ ( $RANDOM % ( $[ 20 - 4 ] + 1 ) ) + 4 ]
    		sleep $SLEEP
    	fi
    done
    When run it compares the public ip address against a file (ipAddress.txt) to basically determine if I am on my VPN or not. If I am not, i.e. it finds my public ip in the ipAddress.txt file it is supposed to exit otherwise it launches firefox with the given URL.

    I have tested this by running from cmd line and running from the crontab. Running from the cmd line I can start the script, let it launch the first URL, then i disconnect from the VPN and the next time it goes through the loop it detects that my VPN has disconnected and kills the script. However, from the cron job if I have disconnected from the vpn I have found that it even though it finds my public ip address within the ipAddress.txt file it still marches right through to the next step in the script and launces the browser and will do this over and over.

    Any idea as to why the checking mechanism would work when I run the script manually from the cmd line and why it behaves differently when cron runs it?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,466
    Rep Power
    1751
    Not the answer to your problem but ... why are you firing up a web browser in cron?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    44
    Rep Power
    10
    Just for fun

    I just need to visit some sites everyday to get credit and wanted to automate it. I can do it manually of course. But lets say im not launching a browser and instead just logging my ip address if my vpn is disconnected. then why does this piece of code behave differently inside of cron vs outside of cron.

    Code:
    #CHECK IP
    	pubIp=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
    	echo "Your public ip is: $pubIp..."
    	pubIpStrip=`echo $pubIp | sed 's/\.[0-9]*$/./' | sed 's/\.[0-9]*$//' | sed 's/\.[0-9]*$/./'`
    
    	compare=`grep -l $pubIpStrip ipAddress.txt`
    	if [ $compare ]; then
    		echo "Your public ip address, $pubIp, is in a bad range.  Do you have your vpn connected?"
    		echo "Now exiting..."
            	exit
    	else
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,466
    Rep Power
    1751
    First off, does $pubIp get set the same when the script is run on command line and via cron?

    I have a sneaking suspicion the issue is with grep. Or rather, which version of grep is being used. When the script runs via cron the $PATH environment variable is set to a very basic value which is likely to differ from the value assigned via .bashrc. That makes is possible (or even likely) that you are actually using different executables when run from cron. From memory 'the usual suspects' with this sort of thing tends to be awk and grep.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    In your shell (now) type
    env > /home/me/shell_env.txt

    At the start of your cron job, add the line
    env > /home/me/cron_env.txt

    Now do
    diff /home/me/shell_env.txt /home/me/cron_env.txt

    Look at PATH, as mentioned by SimonJM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    44
    Rep Power
    10
    $pubIp does get set no matter how I run it, but the grep result is different.

    I did a which grep from the command line and it returns "/bin/grep".

    PATH from cron and shell both have "/bin" directory in them so Im not sure why the difference. But I agree that this seems to be the problem. When I run from the cmd line and I have an ip in $pubIp that I know is in the ipAddress file it returns a value whereas from cron it never returns a value no matter what.

    Thanks for the info. Helped me better understand how cron works.

IMN logo majestic logo threadwatch logo seochat tools logo