LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2015, 02:46 AM   #1
lq_win
Member
 
Registered: Jul 2015
Posts: 48

Rep: Reputation: Disabled
Question get variable value not working in cron job


Hello All,

I am running script below to get default route


CURRENT_GW="$(ip route show | grep default | awk -F' ' '{print $3}')"
and run echo ${CURRENT_GW} then it shows correctly..

now I try to put it into a script (test.sh)

CURRENT_GW="$(ip route show | grep default | awk -F' ' '{print $3}')"
echo "${CURRENT_GW}" >> /tmp/mytest

but it shows empty

what happen with this?..
could any body please help?

thanks a lot in advance

REgards
Win
 
Old 11-02-2015, 03:00 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,475

Rep: Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424
you would need to insert set -xv at the beginning of the script and save all the stdout and stderr of it (and check it).
so in the cronjob you can do something like this:
* * * test.sh >/tmp/test.stdout 2>/tmp/test.stderr
You will surely find the reason. I think ip/grep/awk could not be found, you need to use them with full path, but I'm not really sure about that.
 
Old 11-02-2015, 04:19 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Generally, the $PATH setting inside cron is minimal to say the least. Best practice is to either

1. use full paths to ALL cmds & files used
2. start by setting up the correct PATH value before doing anything.
Ensure you are in the right dir as well; cron does not login in the normal sense

Last edited by chrism01; 11-02-2015 at 04:29 AM.
 
Old 11-02-2015, 04:40 AM   #4
lq_win
Member
 
Registered: Jul 2015
Posts: 48

Original Poster
Rep: Reputation: Disabled
Thanks all
Its works now by adding full path of them
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble setting an environment variable with a cron job PaulB0 Linux - Newbie 5 06-23-2012 03:06 AM
[SOLVED] Cron job not working 2handband Slackware 7 11-14-2010 10:29 AM
cron job not working bittus Linux - Software 7 04-27-2009 08:39 PM
Cron Job Not Working... ALInux Linux - Software 2 07-24-2007 05:15 AM
Cron job not quite working... simba_cubs Linux - General 10 11-08-2006 07:27 AM


All times are GMT -5. The time now is 06:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration