reserving process id (PID)

I am trying to submit a background process on a remote machine using

ssh -f remote-host "sh my-process.sh"

Normally the process will be submitted and control returned to the local machine (even when the process is running on the remote machine)

I want to get the process id of the submitted process on my local machine so that I can track the machines+process- id

How can I get this ?

Or, can I reserve a process ID and then submit the job assigning that particular PID.

Basically I am trying to write my own version of Sun Grid Engine (SGE) so that I have more control

Thanks.
J.

The process id of a child process is only truely atomically valid while the parent is still alive and before the wait() call has reaped it.

If you start a process and it gets the number X, and the parent then dies, when process X dies that same pid X can be reused and you will not know.

Secondly, the parent is the only one who can efficiently tell you when that child has died, as it will get the SIGCHLD and will call wait().

Thanks for the reply. But, it just went above my head...sorry :frowning:
Are there any pointers to this or could you make it a little more simpler
What is the parent process in my case ? PID corresponding to SSH

Just by luck, I found the solution. I gave a & in the command at I get the PID of the submitted job on my local machine.

ssh -f remote-host "sh my-process.sh &"

and it gave me
[1] 10327

10327 is the process id on the remote machine.
What does [1] signify ?

Thanks,

'job' 1. If you use the jobs command, you'll see it's been assigned an id of 1. You can then pass this to the fg and bg commands if you want.

FYI the environment variable $! get populated with the PID of the last command set to run in the background by the current shell or script. Much like $? is the exist code from the last command.

There is a difference in the behaviour of KornShell and Bash you might want to observe: bashs child processes put in background won't be terminated once the process controlling that terminal dies. KornShell processes will, however, die with their parents until they are started with the "nohup" (no hangup [upon loss of terminal]) command.

To explain that in more common terms: when you issue 'ssh -f host "cmd"' you basically open an interactive session like you would log on, issue "cmd" in this session and then close it again. The whole procedure will normally wait until "cmd" is finished. By logging on, you create a (pseudo-)terminal, which will be closed when you log off.

Now consider a background process as "cmd". It is not possible to wait until it finished, because that could possibly be forever. In fact you just want to *start* it, not waiting for its end. So you start a (in fact interactive) shell, start in this shell a process in background and then want to terminate the shell - the child will, if this shell is a KornShell - die with the parent.

What you could do in this case is:

  • start the job in background
  • find out its PID (inside the process environment)
  • issue a "nohup -p PID"
  • terminate the controlling shell - the nohup-ped process will survive

"cmd" in this case would look like (i will use vmstat as an example):

vmstat 10 > logfile & ; PID="$(jobs -l | sed -n 's/^\[[0-9]*\]  *+  *\([0-9]*\) .*$/\1/p') ; nohup -p $PID

The output of "jobs" will decorate the last recently issued background job with a "+", which means, this is the job which will become the foreground job when the command "fg" is issued. The sed-statement just extracts the PID from this line.

HTH

bakunin