LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   timeout core utility (https://www.linuxquestions.org/questions/linux-newbie-8/timeout-core-utility-837595/)

poojithas 10-12-2010 09:10 AM

timeout core utility
 
Hi,

I'm using timeout core utility in my bash shell script. The problem I'm facing is I want the PID of the command which is supplied to timeout. Let me explain with the code I'm using:

Code:

#!/bin/bash

 timeout 120 ./prog1 &
 echo "@$!"

the above script gives me PID of timeout process but not of the prog1, so how do I get that.

What I observed is: prog1 PID = timeout PID + 1
Is it always the case that if I add 1 to timeout PID, I get prog1 PID ?

Tinkster 10-12-2010 04:22 PM

I don't think that's a feasible assumption - it can (and frequently will)
be correct, but it doesn't have to.

What you CAN do is to ps -ef, and find the
process which has the timeout PID as its PPID.

Code:

ps -ef|awk '$3 == PID {print $2}'
Cheers,
Tink

poojithas 10-12-2010 06:53 PM

Great, it works.

Between, Is there any C language function to get parent PID given a child PID ? . I know getppid() but this only gives current process parent pid. I'm looking for a function which can take child pid and gives me parent pid.

Tinkster 10-12-2010 07:25 PM

Quote:

Originally Posted by poojithas (Post 4125413)
Great, it works.

Between, Is there any C language function to get parent PID given a child PID ? . I know getppid() but this only gives current process parent pid. I'm looking for a function which can take child pid and gives me parent pid.

I don't think that's exposed via a function anywhere - but
you can easily implement it yourself by traversing /proc/<PID>/

Either of "stat" and "status" will give you the ppid.



Cheers,
Tink

poojithas 10-12-2010 07:54 PM

Modified script:

#!/bin/bash
timeout 120 ./prog1 &
PID=`ps -eo pid,ppid | awk -v pid="$!" '$2 == pid {print $1}'`
echo "@$PID"

here I'm only supplying pid and ppid from ps command to awk to filter. This is somewhat neat I think.

Tinkster 10-12-2010 08:03 PM

Yup - looks good :}


All times are GMT -5. The time now is 11:21 AM.