문제

I am using the following code to launch a command on another machine:

#!/bin/bash
/usr/bin/rsh -n $Host_Name "cat asdf.txt &"

And I am trying to obtain the PID of the cat command by using the following:

/usr/bin/rsh -n $Host_Name pid="$!"

But when I echo $pid, it is just blank. What am I doing incorrectly? Is there an easier way of obtaining the PID of the last command that was executed on a different machine?

Thanks

도움이 되었습니까?

해결책

You can only get the $! of the backgrounded command in the shell in which you started the command. If your command doesn't output anything to stderr, this could work:

/usr/bin/rsh -n $Host_Name "cat asdf.txt & echo $! >&2" 2> pidfile

The pid of the started command will then be stored locally in 'pidfile'.

Just a side-note: I would never use rsh. It is inherently insecure. I'd use ssh instead ;)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top