Cron 在 cron 内部没有回显正确的值,但外部工作正常

Cron 在 cron 内部没有回显正确的值,但外部工作正常

我什至包含source /home/user/.bashrc; <my scripts>在 crontab 中,但它仍然没有输出正确的东西。

我的脚本中有一个 while 循环,它检查来自名为 zend 的服务的某些值。它就像bitcoind,但针对的是Zen。这个函数看起来像这样,我把它这里 check_existing_balance_withoutfl应该大于 0,但当我从 cron 运行时它说它是 0。

{

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
}

与>sudo crontab -e一起使用时记录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

如果我不是从 crontab 运行脚本,则一切正常。

$ 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

那么,当它从 cron 执行时,为什么 bal 不是 498 呢?我真的不知道我做错了什么。

按照@Marcel的建议查看bash -x日志后,我似乎发现了问题。因为,我将脚本放入 中sudo crontab -e,它引用 /home/root/.zen 目录中的配置文件,因此对于 zend 客户端,我必须替换脚本中的所有zen-cli行。如果有人想要放入crontab 来检查某些特定事务或其他内容,这zen-cli -conf=/home/user/.zen/zen.conf同样适用。bitcoindbitcoin-cli

答案1

按照@Marcel的建议查看bash -x日志后,我似乎发现了问题。

因为,我将脚本放入 中sudo crontab -e,它引用了/home/root/.zen目录中的配置文件。

因此,为了让zend客户端工作sudo crontab -e/etc/crontab我必须将脚本中的所有zen-cli行替换为。zen-cli -conf=/home/<yourusername>/.zen/zen.conf同样适用于bitcoind,

不要使用$USERin,<yourusername>因为它将是 root 下的sudo crontab -e,因此最好使用用户的全名或在变量中指定用户名。

如果有人想使用bitcoin-cliinsudo crontab -e来检查某些特定交易或在系统启动或任何其他功能期间需要密码的东西,您可以替换

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

因此,例如检查最新块而不是执行

bitcoin-cli getblockcount做:

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

相关内容