reserving process id (PID)


 
Thread Tools Search this Thread
Operating Systems Linux reserving process id (PID)
# 1  
Old 12-11-2007
reserving process id (PID)

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

Code:
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.
# 2  
Old 12-11-2007
Quote:
Originally Posted by superuser84
How can I get this ?
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().
# 3  
Old 12-11-2007
Thanks for the reply. But, it just went above my head...sorry Smilie
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.

Code:
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,
# 4  
Old 12-11-2007
'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.
# 5  
Old 12-12-2007
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):

Code:
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

Last edited by bakunin; 12-12-2007 at 02:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

How to get process name from PID?

HI, i used ps -ef | grep 3539052 | grep -v grep and i got a output like ths root 3539052 3407918 0 May 07 - 709:31 /usr/sbin/syslogd but what i need is instead of full path /usr/sbin/syslogd i want only the process name that is 'syslogd' here. (3 Replies)
Discussion started by: sumanthupar
3 Replies

2. HP-UX

PID Process Resources

Hi all. I need to get detailed information about a PID. I have an app called Reflection X and it shows many things like Total CPU percent, User CPU (Nice) percent, FS Reads, etc. My question is how can I get all that information on console? Is there an archive with the detailed... (9 Replies)
Discussion started by: the0m3n
9 Replies

3. AIX

Assigning PID for a Process

Hello Team, Is there anyway to assign a dedicated pid for a particular java process(application). Regards, Gowtham.G (5 Replies)
Discussion started by: gowthamakanthan
5 Replies

4. Programming

[C] Process pid by name

Hi I use linux OS. I've already written a function that allow me to get the process name by pid. (searching in /proc). Now I'd like to perform the inverse task.I mean get the process pid by its name. I could write a function that search in every folder in /proc for the process name, but i... (2 Replies)
Discussion started by: Dedalus
2 Replies

5. Shell Programming and Scripting

getting pid from a process

Hi all, i was able to redirect pid of process to a file in the following way ps aux|awk '$11 == "/Applications/ProjectX/DServer" >> /Applications/ProjectX/DServer.pid it works fine but if one folder name caontains space its not working like below ps aux|awk '$11 == "/Applications/Project\... (1 Reply)
Discussion started by: kirankumars
1 Replies

6. Shell Programming and Scripting

Get an PID of particular process

Hi I have written a shell script to find and kill the particular process. Here in shell script i have written the code like cnt = $(ps -ef | grep Shree) echo $cnt I am getting the output root 2326 2317 0 14:39:46 pts/1 0:28 Shree -f fdc.fbconf FDCapp.fbapp Here I want to... (2 Replies)
Discussion started by: Shreedhar Naik
2 Replies

7. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

8. Solaris

getting pid of process

hi all, Is there a simple script anyone could through out to me, to find the pid of a process given the name. I actually need to bind this pid to a processor set. I would probably put these comamns in a shell script which would have. a) kick start the executable b) get the pid c) bind it to a... (10 Replies)
Discussion started by: Naanu
10 Replies

9. Shell Programming and Scripting

Process PID

Hi Friends :p I have a little problem please help me out. I have a Unix based OS Sun Server having oracle 8i as database on it. The server has one client with windows OS. The client uses developer 2000 (GUI) to run query and run processes. I want to know how can I know the PID of a process run... (3 Replies)
Discussion started by: vanand420
3 Replies

10. Programming

get process name from pid - getcommandline

All, I have a requirement to get the process name of the newly execv'd process. After getting the complete process name, I need to carry out further operations on that. Problem so far is how do I retrieve the name of the process ? There is an API in windows called GetCommandLine. I... (2 Replies)
Discussion started by: vino
2 Replies
Login or Register to Ask a Question

Featured Tech Videos