LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2010, 10:10 AM   #1
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Rep: Reputation: 0
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 ?
 
Old 10-12-2010, 05:22 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 10-12-2010, 07:53 PM   #3
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Original Poster
Rep: Reputation: 0
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.
 
Old 10-12-2010, 08:25 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by poojithas View Post
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
 
Old 10-12-2010, 08:54 PM   #5
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Original Poster
Rep: Reputation: 0
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.
 
Old 10-12-2010, 09:03 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Yup - looks good :}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] DMA Timeout Error in booting up tiny core souren.sinha Linux - Newbie 2 09-10-2010 04:39 AM
core utility lipun4u Programming 2 06-17-2010 03:58 PM
ttmkfdir command/utility in Fedora Core 6??? noble_curious Linux - Distributions 1 11-22-2007 11:13 AM
Fedora Core 5 hangs with ata timeout balusamy Fedora 6 06-15-2006 06:13 PM
Is there a System Restore Utility for Fedora Core 5? YAOMK Fedora 10 05-19-2006 05:33 PM


All times are GMT -5. The time now is 06:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration