0

I even included source /home/user/.bashrc; <my scripts> 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 '.[2]');

    if python -c "import sys; sys.exit(0 if float($check_t_balance) <= float($min_ask_zen) else 1)"; # # for test
    # # if python -c "import sys; sys.exit(0 if float($check_t_balance) >= float($min_ask_zen) else 1)"; 

        # if [ $(bc <<< "$check_t_balance >= $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 sudo 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.

  • 1
    What's your cron entry? Which file is it in - or what command did you use to add it? Do you have a small segment of code that illustrates the problem? Please share that with us here in your question. – roaima Feb 15 at 9:10
  • I used sudo crontab -e I used this 55 8 * * * source /home/rock64/.bashrc; /home/rock64/light.sh > /home/rock64/both.log 2>&1 . I will put @reboot when it works or if it works. I have also tried running it from systemd, same issue – Rakib Fiha Feb 15 at 9:11
  • 1
    execute your script with bash -x script in your cronjob and log the output. – Marcel Feb 15 at 9:12
  • 1
    @RakibFiha Please show sourcecode and output in your question and add all clarification to the question, not in comments or on external servers. – Bodo Feb 15 at 9:52
  • 1
    Pleased you've got a solution. But please don't put your answer in the question. Put it in an answer - and then accept your own answer with the tick mark. That's how these sites work. Thanks – roaima Feb 15 at 12:23
0

After looking at the bash -x log as suggested by @Marcel, it seems, I found the problem.

Since, I put my script in sudo crontab -e, it was referring to a config file in /home/root/.zen directory.

So for the zend client to work under sudo crontab -e or /etc/crontab I had to replace all zen-cli lines with zen-cli -conf=/home/<yourusername>/.zen/zen.conf in my script. Same applies to bitcoind,

Do not use $USER in <yourusername> since it will be root under sudo crontab -e, so rather use the full name of the user or specify in a variable for the username.

If someone ever wants to use bitcoin-cli in sudo crontab -e to check some specific transactions or something that requires password during system bootup or any other functions, you can replace

bitcoin-cli with bitcoin-cli -conf=/home/<yourusername>/.bitcoin/bitcoin.conf

So, for example to check latest block instead of doing

bitcoin-cli getblockcount do:

bitcoin-cli -conf=/home/<yourusername>/.bitcoin/bitcoin.conf getblockcount

  • 1
    So, why do you use sudo crontab? Why aren't you using your own crontab? – glenn jackman Feb 15 at 16:13
  • In the script, there are some systemctl commands which require the sudo password. Is there any alternative to sudo crontab for this scenario? – Rakib Fiha Feb 15 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.