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 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 ? |
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}' Tink |
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. |
Quote:
you can easily implement it yourself by traversing /proc/<PID>/ Either of "stat" and "status" will give you the ppid. Cheers, Tink |
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. |
Yup - looks good :}
|
All times are GMT -5. The time now is 11:21 AM. |