How can i get the parent process only?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How can i get the parent process only?
# 1  
Old 07-09-2008
Bug 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.
# 2  
Old 07-10-2008
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?
# 3  
Old 07-13-2008
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
# 4  
Old 07-13-2008
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.
# 5  
Old 07-13-2008
Quote:
Originally Posted by nirav_soni
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.
# 6  
Old 07-14-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Find parent process (not process ID)

Hi: I have a program written in FORTRAN running on AIX platform. It is because missing of documentation and without root password, therefore we want to modify the program so that we can find out which script/program that call this FORTRAN program. I have google for few days, all of them are... (3 Replies)
Discussion started by: cstsang
3 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

4. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

5. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

6. UNIX for Dummies Questions & Answers

parent process

I need to kill all the processes of user "release" on system "PROterm" or the parent process for the user I did this ps -ef | grep PROterm | grep release i got release 2900014 2961494 0 12:19:44 pts/3 0:00 PROterm release 3264694 2900014 0 12:19:44 - 0:00 PROterm if i... (7 Replies)
Discussion started by: ajit.yadav83
7 Replies

7. UNIX for Dummies Questions & Answers

parent process in unix

hello i am not a unix programmer but i need to solve a particular problem using unix. suppose i have a user XYZ logged on to a system called ABC. i want to get the parent process id for the user and kill it i am using ps -ef | grep XYZ | grep ABC it returns me all the processes for... (2 Replies)
Discussion started by: ajit.yadav83
2 Replies

8. UNIX for Dummies Questions & Answers

Who is the parent of a killed process ?

Suppose we have the following process tree: init-> ProcessA->processB->processC then I kill processB Who is the parent of the processC? init or the processA (6 Replies)
Discussion started by: Puntino
6 Replies

9. Shell Programming and Scripting

how to find the chid process id from given parent process id

how to find the chid process id from given parent process id.... (the chid process doesnot have sub processes inturn) (3 Replies)
Discussion started by: guhas
3 Replies

10. UNIX for Dummies Questions & Answers

identify parent process in ps?

I need to write a script to monitor user processes that revert to parent process id = 1 I can do this with a grep " 1 " or awk and evaluation. but Is there a switch on the ps command or another command to specify parent id on proccesses? I tried ps with the -p ppid=nnnnn but it... (6 Replies)
Discussion started by: MizzGail
6 Replies
Login or Register to Ask a Question