how to grep parent process PID in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to grep parent process PID in shell scripting
# 1  
Old 06-25-2009
Error how to grep parent process PID in shell scripting

hi all,

for an example:
$ ps -ef | grep apache | awk '{ print $2, $3 }'
24073 11784
28021 1
28022 1
28038 1
28041 28040
28045 28041
28047 28041
28040 1
28049 28041
28051 28041
28053 28041
28030 1
28054 28041
28055 28041
28056 28041
28057 28041
28061 28039
28065 28041
28066 28041
28067 28041
28068 28041
28069 28041
28070 28041
28071 28041
28078 28041
28080 28041

I want to display only the second columns showing "1" assocaited PID's
in our case output should show:
28021 1
28022 1
28038 1
28040 1
28030 1
# 2  
Old 06-25-2009
Extending your awk statement.

Code:
 
$ ps -ef | grep apache | awk '{ if($2==1) {print $2, $3 }}'

# 3  
Old 06-25-2009
replace awk with...
Code:
awk '$3==1{print $2 $3}'



---------- Post updated at 05:47 PM ---------- Previous update was at 05:46 PM ----------

Quote:
Originally Posted by panyam
Extending your awk statement.

Code:
 
$ ps -ef | grep apache | awk '{ if($2==1) {print $2, $3 }}'

I think he needs $3 as 1 and not $2, please see.
# 4  
Old 06-25-2009
Yes Smilie ...
# 5  
Old 06-25-2009
Computer

panyam thanks a ton!!!
it works as I expected..
# 6  
Old 06-25-2009
Quote:
Originally Posted by panyam
Extending your awk statement.

Code:
 
$ ps -ef | grep apache | awk '{ if($2==1) {print $2, $3 }}'

no need for grep AND awk:
Code:
ps -ef | awk '/apache/ && $3==1 { print $2, $3 }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the parent PID only in Solaris?

Hi, I am using this command fuser_result=`fuser -f /web/$1/admin-*/logs/access` to get the parent pid of the process. However, The output differs and at times it shows two pids instead of one thus failing the logic of my script. See output below: bash-3.2$ fuser -f... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

3. 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

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

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

6. 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

7. Shell Programming and Scripting

Find shell process pid launched throug `at`.

Hi. I was testing some staff and wrote simple script, which only writes date to log every 15 seconds. Like that #1.sh while true;do echo `date` >> 1.log sleep 15 done And than i ran this process with `at -s -f 1.sh now`. And now it is running and i don't know how to catch it. I tryed... (1 Reply)
Discussion started by: kukuruku
1 Replies

8. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

9. Shell Programming and Scripting

Environment Variable Parent PID

I have always used the "$$" environment variable to find the current process number. Is there any similar way or perhaps something else to easily find the parent process number? I realize I could do something like ps and grep for the process and cut or awk out the parent process but I wanted to... (1 Reply)
Discussion started by: scotbuff
1 Replies

10. Shell Programming and Scripting

parent shell pid

hi folks can any suggest me how to get a parent processid in the script if i am executing this in the script vi myscript.sh echo "parent shell pid"$$ sh myscript.sh but when i am executing this i am getting a new pid(actually that is the child pid) whenever i am executing this... (4 Replies)
Discussion started by: maheshwin
4 Replies
Login or Register to Ask a Question