The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print parent process name mahendramahendr Shell Programming and Scripting 3 05-23-2008 04:59 AM
Who is the parent of a killed process ? Puntino UNIX for Dummies Questions & Answers 6 05-13-2008 01:50 AM
Can a child process return a specific value to a parent process ? Ametis1970 High Level Programming 8 04-09-2008 08:22 PM
how to find the chid process id from given parent process id guhas Shell Programming and Scripting 3 10-13-2005 05:13 AM
identify parent process in ps? MizzGail UNIX for Dummies Questions & Answers 6 07-03-2002 11:37 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2008
Registered User
 

Join Date: Nov 2007
Posts: 12
Smile How can i get the parent process only?

Hello,

I'm running one script every night and that's kick off through cron jobs.

Inside the script i'm checking whether my script is already running or not.

I'm using following command to check whether is running or not.

ps -eaf | grep "test_PID.ksh" | grep -v "grep test_PID.ksh" | wc -l

sometime it gives me 2 process or 3 process

Output:
user 15243 15203 0 12:00:01 ? 0:00 /usr/bin/ksh /usr/local/my/bin/test_PID.ksh

user 15203 254 0 12:00:01 ? 0:00 sh -c /usr/local/my/bin/test_PID.ksh

Why am i seeing two process for one job.

How can i get only parent process not child process.

Thank you for your helps in adv.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-10-2008
Registered User
 

Join Date: Dec 2007
Posts: 208
why not create a scriptname.PID while the script is running. At the end of the scirpt it can remove this. Then the script can check for the existing of the .PID file and exit if it exists.

thought?
Reply With Quote
  #3 (permalink)  
Old 07-12-2008
Registered User
 

Join Date: Jul 2008
Posts: 7
Try this :

ps -p $$ -o %P | sed -n 's/^ *\([0-9][0-9]*\) *$/\1/p'

Where $$ --> Is the process name whose Process ID you want to get.

Hope that helps

Cheers,
Kunal
Reply With Quote
  #4 (permalink)  
Old 07-13-2008
Registered User
 

Join Date: Apr 2006
Location: Northern Virginia
Posts: 23
Process management

I think you're seeing two due to your invocation method. One method that would be more "tight" would be to have your process write it's PID to a PID file, or have the script write it if necessary. Then check to see if the PID is running, rather than guessing by checking for the existence of a file. ABEND-ing programs rarely clean up after themselves. PID's are either in use or not. The only possible hole in this logic is that PID's are reused periodically as the PID integer is incremented through it's max. The likelihood of that error is quite very very low.
Reply With Quote
  #5 (permalink)  
Old 07-13-2008
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Quote:
Originally Posted by nirav_soni View Post
Output:
user 15243 15203 0 12:00:01 ? 0:00 /usr/bin/ksh /usr/local/my/bin/test_PID.ksh

user 15203 254 0 12:00:01 ? 0:00 sh -c /usr/local/my/bin/test_PID.ksh

Why am i seeing two process for one job.

How can i get only parent process not child process.

Thank you for your helps in adv.
You're seeing two processes because of the way cron runs jobs. It uses 'sh', so the "sh -c" is the actual process that cron has run. However, your script probably starts with "#! /usr/bin/ksh" so you are seeing the second process. A way around this, as has been suggested by other posters, is to have your script echo its current pid to a file. Something like "echo $$ > /path/to/somefile" will probably be enough. That allows you to check your own pid anytime you want.
Reply With Quote
  #6 (permalink)  
Old 07-14-2008
Registered User
 

Join Date: Apr 2006
Location: Northern Virginia
Posts: 23
One additional note, adding to the astute comments by my esteemed colleague blowtorch. Try looking at "exec", which makes the parent's start of the child process fatal.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0