3

リモートプロセスを非同期で実行し、そのリモート pid、出力 (stdout + stderr) をファイルまたは変数 (さらに処理するために必要) に保存し、コードを終了したいと考えています。

リモート pid は、リモート プロセスが完了した後ではなく、実行中に必要になります。また、同じ名前の複数のプロセスがリモート マシンで実行されるため、プロセス名を使用するソリューションは機能しません。

私がこれまでに持っているもの:

export SSH="ssh -o ServerAliveInterval=100 $user@$remote_ip"

「my_test」は実行したいバイナリです。

私が試したリモートpidと出力を取得するには:

$SSH "./my_test > my_test_output & echo \$! > pid_file"
remote_pid=$($SSH "cat pid_file")
# run some remote application which needs the remote pid (send signals to my_test)
$SSH "./some_tester $remote_pid"
# now wait for my_test to end and get its exit code
$SSH "wait $remote_pid; echo $?"
bash: wait: pid 71033 is not a child of this shell

$SSH コマンドは、この ssh ソケット ( https://unix.stackexchange.com/a/30433/316062 )に接続されているファイル記述子がないため、リモート pid を pid_file にエコーした後に戻ります。

どうにか my_test 終了コードを取得する方法はありますか?

4

1 に答える 1

0

OK、私の解決策は次のとおりです。

    # the part which generates the code on the remote machine
    $SSH << 'EOF' &
    ./my_test > my_test_output &
    remote_pid=$!
    echo $remote_pid > pid_file
    wait $remote_pid
    echo $? > exit_code
    EOF
    local_pid=$!

    # since we run the previous ssh command asynchronically, we need to make sure
    # pid_file was already created when we try to read it
    sleep 2
    remote_pid=$($SSH "cat pid_file")
    # now we can run remote task which needs the remote test pid
    $SSH "./some_tester $remote_pid"
    wait $local_pid
    echo "my_test is done!"
    exit_code=$($SSH "cat exit_code")
于 2019-07-04T07:54:04.850 に答える