i have simple bash script working manual @ terminal, after cron gives empty variable.
#!/bin/bash gwip=`/usr/bin/nmcli dev list iface eth0 | grep ip4-settings.gateway: | awk '{ print $2}'` printf '%s\n' "$(date) =- $gwip -= " >> /var/log/looog.log
run: /bin/bash /test.bash
output in file /var/log/looog.log:
1 monday 2016 14:17:36 +0300 =- 23.18.117.254 -=
when run through cron, variable empty.
*/1 * * * * root /bin/bash /test.bash
output in file /var/log/looog.log:
1 monday 2016 14:19:13 +0300 =- -=
why variable $gwip empty? how fix it?
qualifying /usr/bin/nmcli
isn't enough -- you're calling bunch of other tools need found path also.
also, in general -- when debugging cron job, arrange stderr go file, so:
#!/bin/bash # log stdout , stderr 2 different files exec >>/var/log/looog.log 2>>/var/log/looog.err.log # ...and log every command try execute stderr (aka looog.err.log) set -x # set path variable export path=/bin:/usr/bin # original code here, using modern posix $() syntax, vs old hard-to-nest `` gwip=$(nmcli dev list iface eth0 | awk '/ip4-settings[.]gateway:/ { print $2}') printf '%s\n' "$(date) =- $gwip -= "
the key things here explicitly-set path (not having value expect set in path common issue in cron jobs), , stderr log (which ensures other issues can identified reading contents).
note use of single redirection looog.log
up-front. doesn't make significant difference when you're literally running 1 print statement, if have extended script have more one, it's more efficient open output file 1 re-open every time have write.
Comments
Post a Comment