LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   get variable value not working in cron job (https://www.linuxquestions.org/questions/linux-newbie-8/get-variable-value-not-working-in-cron-job-4175557751/)

lq_win 11-02-2015 01:46 AM

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

pan64 11-02-2015 02:00 AM

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.

chrism01 11-02-2015 03:19 AM

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

lq_win 11-02-2015 03:40 AM

Thanks all
Its works now by adding full path of them


All times are GMT -5. The time now is 11:17 PM.