How to get PID of a backgroung prsc from that process???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get PID of a backgroung prsc from that process???
# 1  
Old 06-01-2009
How to get PID of a backgroung prsc from that process???

It seems obviose, but I surprised to be not able to get it!

In bash, expected to get a current process ID by '$$'
So, if I ran backgroung any stuff and try from there get that process ID I would expect to have the same number as I have reported on foregroung screen, when I have started that background!
But it is not right!
Code:
/src> echo $$
14001
/src> { sleep 1; echo "sleepy-$$"; sleep 1; } &
[1] 8106
/src>
/src> sleepy-14001

[1]+  Done                    { sleep 1; /usr/bin/echo "sleepy-$$"; sleep 1; }

See, the backgroung process return the PID of parent process?! - 14001, while I would expect the 8106 ?!?!

How I should get the '8106' from inside of the curved brases?

The reason is that I need to run concurently many processes, which should use the same functions that need to use own files. I need separate files between processes and timestamp is not usefull.
I have plan to use the PID but..!!!

What I understand wrong about PIDs?
How I could get PID of any backgroung process from that process?

Thank you!
# 2  
Old 06-02-2009
who said that a process will run with same PID when its ran in FG and in BG.??
# 3  
Old 06-02-2009
Quote:
Originally Posted by alex_5161
It seems obviose, but I surprised to be not able to get it!

In bash, expected to get a current process ID by '$$'
So, if I ran backgroung any stuff and try from there get that process ID I would expect to have the same number as I have reported on foregroung screen, when I have started that background!
But it is not right!
Code:
/src> echo $$
14001
/src> { sleep 1; echo "sleepy-$$"; sleep 1; } &
[1] 8106
/src>
/src> sleepy-14001

[1]+  Done                    { sleep 1; /usr/bin/echo "sleepy-$$"; sleep 1; }

See, the backgroung process return the PID of parent process?! - 14001, while I would expect the 8106 ?!?!

How I should get the '8106' from inside of the curved brases?

Have you read the bash man page? If you search it for $$, you will find BASHPID.

Code:
{ sleep 1; echo "sleepy-$BASHPID"; sleep 1; } &

# 4  
Old 06-02-2009
PID 14001 is your process id for the current SHELL
8106 is the PID of your background command.

use $! to get the PID of the last background job executed.
Code:
echo $!

-Devaraj Takhellambam
# 5  
Old 06-02-2009
Does anyone know the way to get any process PID inside of that process?

Ok, example:
Exist a function FUNK that in processing create temporary file and call other functions that use that file.

From process-1 I start background processed B1, B2, B3

Each call the FUNC

I need to be able to separate the files that FUNC will create.

That why I need the PID of each process INSIDE of that process! NOT in the parent process where B1,B2,B3 has been started.
I need the FUNC be able to get the B1 PID when it is running in B1, B2 PID when the FUNC is called in B2 and B3 ... - the same.

(I do not see a reason to reply on 'very constructive' responses as 'if I ever read manual', or on pretention that I expect the same PID, or on advise to get it by parent process )
# 6  
Old 06-02-2009
Quote:
Originally Posted by alex_5161
(I do not see a reason to reply on 'very constructive' responses as 'if I ever read manual', or on pretention that I expect the same PID, or on advise to get it by parent process )

Did you read my post?

I repeat:

Code:

{ sleep 1; echo "sleepy-$BASHPID"; sleep 1; } &

# 7  
Old 06-02-2009
Quote:
Originally Posted by cfajohnson

Did you read my post?

I repeat:

Code:

{ sleep 1; echo "sleepy-$BASHPID"; sleep 1; } &

cfajohnson - if you mean that the $BASHPID should give me the current PID - that is not clear from your statement.
Although I have check that, and my shell does not have this variable defined.
Also I have not found this variable in bash manual.
Code:
/src> echo $BASHPID

/src> echo "bacgroung : $BASHPID" &
[1] 4173
bacgroung :
[1]+  Done                    echo "bacgroung : $BASHPID"
/src>

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. Shell Programming and Scripting

Get PID of a process into a variable

Hi All, I need to get the PID of a process which i ran in background into a variable echo $! gives me the PID of last background process but how to get this into a variable so that i can use "wait" command later in the script to make sure that this background process was done var = `echo... (5 Replies)
Discussion started by: firestar
5 Replies

3. 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

4. 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

5. 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

6. 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

7. Linux

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... (4 Replies)
Discussion started by: superuser84
4 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