远程启动后台启动作业的pid

dav*_*nio 5 ssh bash

我试图在后台启动一个远程机器上的工作并获得它的PID,以便我可以在以后杀死它.到目前为止我想出的是以下内容:

#!/bin/bash

IP=xxx.xxx.xxx.xx
REMOTE_EXEC="ssh $IP -l root"

# The following does NOT work, I am trying to get the PID of the remote job
PID=`$REMOTE_EXEC 'vmstat 1 1000 > vmstat.log & ; echo $!'`

# Launch apache benchmark
ab -n 10 http://$IP/

$REMOTE_EXEC "kill $PID"
Run Code Online (Sandbox Code Playgroud)

不幸的是它不起作用.我得到了一个

bash: syntax error near unexpected token `;'
Run Code Online (Sandbox Code Playgroud)

但我不知道正确的语法是什么.

小智 3

你得到了错误,因为你';' 是多余的,尝试'vmstat 1 1000 > vmstat.log & echo $!'

但我不确定它会起作用,因为注销后,该进程会收到 SIGHUP。看看 nohup(1)。