linux - Debian bash script works fine from command line; fails in crontab -



linux - Debian bash script works fine from command line; fails in crontab -

i'm trying utilize dreamhost api (see here) update dns entry using crontab. have written next bash script:

#!/bin/bash dh_api_key=<my api key> dh_domain=<my fq subdomain> dh_url="https://api.dreamhost.com/?key=$dh_api_key&record=$dh_domain&type=a" ip_addr=`ifconfig eth0 | grep "inet addr" | grep -o "\([0-9]\{1,3\}\.\?\)\{4\}" | head -n 1` old_ip_addr=`wget -q -o - ""$dh_url&cmd=dns-list_records"" | grep $dh_domain | grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"` dh_url="https://api.dreamhost.com/?key=$dh_api_key&record=$dh_domain&type=a" dh_url_add="$dh_url&cmd=dns-add_record&value=$ip_addr" dh_url_del="$dh_url&cmd=dns-remove_record&value=$old_ip_addr" echo $ip_addr echo $old_ip_addr if [ "$ip_addr" == "$old_ip_addr" ] echo same else echo different; updating wget -q -o - "$dh_url_del" wget -q -o - "$dh_url_add" fi

my crontab entry looks this:

0,15,30,45 * * * * /home/<username>/dreamhost_api/dns_update_script > /home/<username>/dreamhost_api/dns_update_script.out # <-- note redirection

if run script manually, next output:

<old ip address> <new ip address> different; updating

... , updates should. if allow crontab run job instead, update never happens. when @ dns_update_script.out file, see:

<blank line> <blank line> same

so script seems have nil ip_addr , old_ip_addr variables when script run crontab (which of course of study means update never happens). thought why? piping not work (or have caveats?) when used in crontab?

since both $ip_addr , $old_ip_addr ending blank when runs cron, perhaps turning on xtrace , examining resulting output cron invocation give clue on what's not working.

putting

set -x

before ip_addr= line should trick. allow cron run , should bunch of output in resulting email (or redirect output user mentioned. bearing in mind xtrace emits stderr, utilize like

* * * * * script >/tmp/outputfile 2>&1

linux bash scripting io-redirection piping

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -