LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 01:46 AM   #1
lq_win
Member
 
Registered: Jul 2015
Posts: 66

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, 02:00 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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, 03:19 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 03:29 AM.
 
Old 11-02-2015, 03:40 AM   #4
lq_win
Member
 
Registered: Jul 2015
Posts: 66

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


Reply



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 02:06 AM
[SOLVED] Cron job not working 2handband Slackware 7 11-14-2010 09:29 AM
cron job not working bittus Linux - Software 7 04-27-2009 07:39 PM
Cron Job Not Working... ALInux Linux - Software 2 07-24-2007 04:15 AM
Cron job not quite working... simba_cubs Linux - General 10 11-08-2006 06:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:00 AM.

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
Open Source Consulting | Domain Registration