Sample Header Ad - 728x90

Cron is not echoing correct value inside cron but outside works fine

0 votes
1 answer
416 views
I even included source /home/user/.bashrc; in crontab, still it does not output correct thing. I have a while loop in my script which checks some value from a service called zend. It is like bitcoind but for Zen. The function looks like this, I put it here check_existing_balance_withoutfl should be greater than 0 but it is saying that it is 0 when I run from cron. { declare -i bal; bal=0 while [ $bal -le 1 ] do # #code to send zen to z addresses echo "Balance is $check_t_balance"; logger "Balance is $check_t_balance"; sleep 5 # This following line is essential at this particular place export_address=$(zen-cli listaddresses | jq -r '.'); if python -c "import sys; sys.exit(0 if float($check_t_balance) = float($min_ask_zen) else 1)"; # if [ $(bc = $min_ask_zen") -eq 1 ]; then #statements echo "ZEN balance is sufficient" echo echo "We have received $check_t_balance zen in $export_address this t address" echo echo "Now, this will be sent to two z-addresses" && echo logger "Now, this will be sent to two z-addresses" && echo # # Recommended tx_fee is >= 0.0001 # # Increse here if needed # tx_fee=0.0001; amt_aft_txfee=$(python -c "print(float($check_t_balance-$tx_fee))"); amt=$(python -c "print(float($amt_aft_txfee/2))"); echo "Sending now.... $amt ZEN to two z addresses" topUpzksnark; # # zen-cli z_sendmany $new_imported_address '[{"address": "'$(zen-cli z_getnewaddress)'", "amount": $amt},{"address": "'$(zen-cli z_getnewaddress)'", "amount": $amt}]'; echo "$amt ZEN is sent to two Z addresses" else echo "ZEN balance is not suffiecient" moreZentoSend=$(python -c "print(float($min_ask_zen-$check_t_balance))") echo "Please send at least $moreZentoSend Zen to $export_address this address" >> /home/rock64/log.txt logger "Please send at least "$moreZentoSend" Zen to "$export_address" this address" echo "bal is $bal" >> /home/rock64/log.txt fi declare -i bal; declare -i check_existing_balance_withoutfl; echo "check_existing_balance_withoutfl is $check_existing_balance_withoutfl" >>/home/rock64/log.txt; check_existing_balance_withoutfl=$(zen-cli z_gettotalbalance | grep total | tr -d '," ' | cut -d ':' -f2 | tr -d '.' | bc); # check_existing_balance_withoutfl=$((10#$(zen-cli z_gettotalbalance | grep total | tr -d '," ' | cut -d ':' -f2 | tr -d '.'))); # check_existing_balance_withoutfl=$((10#$check_existing_balance_withoutfl)); bal+=$check_existing_balance_withoutfl; echo "check_existing_balance_withoutfl is $check_existing_balance_withoutfl" >>/home/rock64/log.txt; echo "bal is $bal after if else" >>/home/rock64/log.txt done } logs when sudo crontab -e used with 55 8 * * * source /home/rock64/.bashrc; /home/rock64/light.sh > /home/rock64/both.log 2>&1> check_existing_balance_withoutfl is 0 bal is 0 after if else There is not enough balance in the node T Address check_existing_balance_withoutfl is 0 check_existing_balance_withoutfl is 0 bal is 0 after if else If I run the script not from crontab, everything works normal as usual. $ check_existing_balance_withoutfl=$(zen-cli z_gettotalbalance | grep total | tr -d '," ' | cut -d ':' -f2 | tr -d '.' | bc) rock64@cheese:~$ echo $check_existing_balance_withoutfl 498 Why bal is not 498 then, when it was executed from cron? I have literally no idea, what is wrong I am doing. After looking at the bash -x log as suggested by @Marcel, it seems, I found the problem. Since, I put my script in
crontab -e
, it was referring to a config file in /home/root/.zen directory, so for the zend client I had to replace all zen-cli lines with zen-cli -conf=/home/user/.zen/zen.conf in my script. Same applies to bitcoind, if someone ever wants to put bitcoin-cli in crontab to check some specific transactions or whatever.
Asked by Rakib Fiha (650 rep)
Feb 15, 2019, 09:05 AM
Last activity: Feb 15, 2019, 12:49 PM