1

I have this script running on a Juniper router which essentially is running Freebsd 10. Problem i am facing is the when cronjob is running this script variable ($current_mem) doesn't show any value as a result logger is also not able to log that value. Anyone have any clue what is going on.

#!/bin/bash
localpid=$$
renice -n +1 $localpid &
current_mem="test"
echo "$current_mem" <<<<shows right value 
current_mem=$(cli -c "show task memory" | grep "Currently In Use" | awk 
'{print $5}' | grep -o '[0-9]*')
echo "$current_mem" <<<<<<<<<<"when cron is running it show's nothing"
if [ "$current_mem" -gt "65" ]
then
echo "$current_mem" <<<<<"shows nothing"
logger -t JTASK_OS_MEM_HIGH -p "external.notice" "Using more then 65 
percent of available memory Current utilization:$current_mem" <<<<<

else
echo "$current_mem"
logger -t JTASK_OS_MEM_NORMAL -p "external.notice" "Using less then 65 
percent of available memory Current utilization: $current_mem"

fi

When i run this script with sh task_mem.sh, script works perfectly but when i run it through cron it doesn't show/dump the value of variable. This what i have for the cron job

# crontab -l
*/1 * * * * sh /var/tmp/task_mem.sh >> 
/var/tmp/task_mem_cron.log
  • Is the `<<<<<<<` something you've added later? This is not valid syntax and will cause the script to fail. – that other guy Sep 21 '18 at 03:10
  • 3
    Are the `cli` and `logger` executables in /bin or /usr/bin? If not, it probably can't find them because cron jobs run with a very minimal environment (including `PATH`). See my answer [here](https://stackoverflow.com/questions/44731613/running-script-in-crontab-reboot-command-not-found). – Gordon Davisson Sep 21 '18 at 04:35
  • Try this to debug your cron: https://stackoverflow.com/a/49296678/1135424 – nbari Sep 21 '18 at 04:44
  • Also use `-x` for debug mode. So, at the start of your script change this line `#!/bin/bash -x` – Ashutosh Sep 21 '18 at 04:58
  • @thatotherguy I added <<<<<< just to show where the problem is – student_XML786 Sep 21 '18 at 18:01
  • @GordonDavisson that was it cli was in /usr/sbin/cli not in /bin as a result cron was not able to execute it. Once i added the full path it started working. Thank you so much for your help – student_XML786 Sep 21 '18 at 18:15
  • @student_XML786 Nice, I'm glad it's solved. You can consider using shell script comments like `# Shows right value` next time since `<<< somestring` is valid bash syntax used for here documents – that other guy Sep 21 '18 at 18:27
  • I'm voting to close this question as off-topic because this is a question about a FreeBSD cron job which should be asked instead on unix.stackexchange.com – Rob Sep 22 '18 at 17:38

1 Answers1

1

@GordonDavisson that was it "cli" was in /usr/sbin/cli not in /bin as a result cron was not able to execute it. Once i added the full path it started working. Thank you so much for your help