Cronはcronの内部で正しい値をエコーし​​ませんが、外部ではうまく機能します。

Cronはcronの内部で正しい値をエコーし​​ませんが、外部ではうまく機能します。

crontabにも含めましたが、source /home/user/.bashrc; <my scripts>まだ正しい結果が出力されていません。

私のスクリプトには、zendというサービスでいくつかの値をチェックするwhileループがあります。ビットコインに似ていますが、Zenの場合です。機能はこんな感じです。ここ check_existing_balance_withoutfl0より大きくなければなりませんが、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問題が見つかったようです。スクリプトを入れたので、/home/root/.zenディレクトリの設定ファイルを参照するので、zendクライアントの場合はスクリプトのすべてのsudo crontab -e行を置き換える必要があります。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-clizen-cli -conf=/home/<yourusername>/.zen/zen.confにも適用されますbitcoind

$USERin<yourusername>はrootの下にあるので使用しないでくださいsudo crontab -e。したがって、ユーザーのフルネームを使用するか、変数にユーザー名を指定することをお勧めします。

誰かが特定の取引、システムの起動、またはその他の機能中にパスワードが必要な項目を確認するためにbitcoin-cliinを使用したい場合は、次のものを置き換えることができます。sudo crontab -e

bitcoin-cliそしてbitcoin-cli -conf=/home/<yourusername>/.bitcoin/bitcoin.conf

たとえば、次の手順を実行する代わりに、最新のブロックを確認します。

bitcoin-cli getblockcountする:

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

おすすめ記事