简体   繁体   中英

Run remote process asynchronically on bash and get its remote pid, stdout+stderr in a file and exit code

I want to run a remote process asynchronically and get its remote pid, output (stdout + stderr) saved into a file or a variable (I need it for further processing) and exit code.

The remote pid is needed while the the remote process is running, not after it's done. Also, multiple processes with the same name run on the remote machine, so any solution which uses the process name won't work for me.

What I've got so far:

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

and "my_test" is the binary I want to run.

To get the remote pid and output I tried:

$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

The $SSH command returns after echoing the remote pid to pid_file, since there are no file descriptors connected to this ssh socket ( https://unix.stackexchange.com/a/30433/316062 ).

Is there a way to somehow get my_test exit code?

OK, my solution is:

    # 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM