Give actual pid as argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Give actual pid as argument
# 1  
Old 11-29-2010
Give actual pid as argument

Hi,

I run a command in the shell that run another process. I need a way to provide the command's pid to the subprocess. For instance, if I run the command foo:
Code:
foo process_to_run -p foo's_pid

I would like to pass to process_to_run the pid of the foo command, or something that link process_to_run to foo.
I would like to access to foo from process_to_run

Is there any way to do that?
Thanks
D
# 2  
Old 11-29-2010
That's an interesting request. It's hard to know your PID in advance!

But not impossible: The exec command can be used to replace your shell's PID with a command. The process being executed will change but the PID won't.

So if you do this inside its own independent little shell script, it should be doable like this:

Code:
#!/bin/sh

exec foo process_to_run -p $$
# Unless exec fails somehow, no commands below here will be run.
# The shell will no longer exist, having been replaced by foo!
echo "exec failed code $?" >&2
exit 1

Where $$ is the shell's PID. Since we're running foo with exec, the shell will be replaced with foo, and have the precise same PID as it when all is said and done.

Last edited by Corona688; 11-29-2010 at 12:36 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-29-2010
That's great!

Thanks a lot
This User Gave Thanks to Dedalus For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SVN for actual VirtualHost

hi, Earlier I had this thread posted on "UNIX for Experts" Group here in unix.com, but somehow no one bothered to respond, so I thought someone might be able to help me here. In short, I have to make accessible a directory via SVN to all 5 developers, call it /var/www/html/beta3 ... (0 Replies)
Discussion started by: busyboy
0 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

4. Filesystems, Disks and Memory

Does vmstat -d give a count of actual physical writes/reads done to/from hard disk?

Hi, I am trying to find the reliability of 'vmstat -d' for showing the actual physical writes on sectors on hard disk. Can anyone please tell me if the numbers in the "sectors" field under "read" or "write" headers show a count of the actual write commands sent to disk from the low level... (2 Replies)
Discussion started by: jake24
2 Replies

5. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 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. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

8. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies

9. Solaris

Need actual bootpd for solaris

Hi everybody, i have downloaded some archieves but i couldn't compile it without errors. please help... where can i find a bootp that works?! my hardware: sparc prozessor with solaris 8 (5.8 (2.8)) thanks Johnny (0 Replies)
Discussion started by: johnnypark
0 Replies

10. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies
Login or Register to Ask a Question