我想异步运行远程进程并将其远程 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 命令在将远程 pid 回显到 pid_file 后返回,因为没有文件描述符连接到此 ssh 套接字 ( https://unix.stackexchange.com/a/30433/316062 )。
有没有办法以某种方式获取 my_test 退出代码?