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?