Visit Our UNIX and Linux User Community


Question related to 'ps'


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question related to 'ps'
# 1  
Old 05-31-2012
Question related to 'ps'

If I run a script called 'abc.sh' and then execute the following :

ps -ef | grep 'abc.sh'

I always get two rows of output, one for the executing script, and the other for the grep command that I have triggered after the pipe.

Questions: Why does the second row turn up in the results. My (flawed) understanding of how the above should work is :
1. ps -ef returns a list of all processes running on the machine
2. grep works on that list to fetch processes with abc.sh in them

So when 'ps -ef' is triggered in step 1, 'grep' isn't running. Then why does it turn up in the results. Can someone explain what happens in the background when two commands are piped?
# 2  
Old 05-31-2012
# 3  
Old 05-31-2012
Thanks for the reply. Is there any way to mark this thread as closed?
# 4  
Old 05-31-2012
There is no option to close this thread. (Only admin and moderate people can do it)

It will be opened and others also express their views. ( May be you will some other answers - which is not in the above link )
# 5  
Old 05-31-2012
If you think about your question, it will logically make sense. You are "grepping" for a name or word and at the time the ps and grep run, there are two process that contain the target word, the process itself and the grep command, hence they are both returned by the grep command.

If it really bothers you, you can add a second pipe to ignore the grep:

Code:
ps -ef| grep 'abc.sh'| grep -v grep

(author's note: boy, talk about a run on sentence. Smilie )

Last edited by de_day; 05-31-2012 at 10:55 AM.. Reason: comment on my own long sentence.
# 6  
Old 05-31-2012
A better way is to change the pattern so it doesn't match itself.

Code:
ps aux | grep '[m]yprocessname'

The [m] matches 'myprocessname' but doesn't match '[m]yprocessname'.
# 7  
Old 05-31-2012
Thanks for your suggestions. I actually know how to avoid the grep process in the output but was curious about the reason it turned up in the output at all.

While this thread is open, I have a lingering question. This code actually runs in one my scripts in a never ending loop to check whether the script is still running. To compensate for the extra grep command in the output, I actually made the code such that it checked for 2 lines in the output of the aforesaid combination of commands, i.e.

Code:
if [ `ps -ef | grep 'abc.sh'` -le 1 ]; then
 echo "Script ends"
fi

This code runs in a never ending while loop. Problem is, the o/p is not consistent. Sometimes, after ten-fifteen minutes of expected results, the loop terminates and echoes the message indicating the script has ended, while in the background the script still runs. I know that this way to monitor script's running status is probably not the best way to do it. But still, why would the output be inconsistent? Does this have to do with multiprocessor setup? Are there instances where ps -ef command has been triggered, and the grep command has still not been triggered? As per the linked article above, the processes might "wait" for their turn, but are fired together.

Last edited by jawsnnn; 05-31-2012 at 06:29 PM.. Reason: linguistic brush ups :)
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #550
Difficulty: Medium
The string concatenation operator can sometimes be combined with non-string types; numerical types.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question related to grep

We have huge file with control A as delimiter. Somehow one record is corrupted. This time i figured it out using ETL graph. If future , how to print only bad record. Example Correct record:... (2 Replies)
Discussion started by: srikanth38
2 Replies

2. Shell Programming and Scripting

awk related question

awk -F ";" 'FNR==NR{a=$1;next} ($2 in a)' server.list datafile | while read line do echo ${line} done when i run the above, i get this: 1 SERVICE NOTIFICATION: nagiosadmin skysmart-01.sky.net .... instead of: SERVICE NOTIFICATION: nagiosadmin skysmart-01.sky.net .... can... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Perl related question

hi, iam perl begginer,i have written the program #!/usr/bin/perl #use warnings; use strict; print "Enter the name:","\n"; my $name=<STDIN>; my %hash=(siva => "9902774481", dev => "9916391244", venky => "9440506760", manohar => "9440232695" ); print "$name no is:... (5 Replies)
Discussion started by: siva.hardwork
5 Replies

4. Shell Programming and Scripting

awk related question

awk "/^<Mar 31, 2012 : /,0" /app/blah.log can someone please help me figure out why the above command isn't pulling anything out from the log? basically, i want it to pull out all records, from the very first line that starts with the date "Mar 31, 2012" and that also has a time immediately... (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

having df command related question

Hi All, When i have run the below command its showing 90% which is critical for production. for this i need the answer of some below question please help me for that. 1) i want to delete some unwanted files. how can i know the unwanted files ?Is it there any way of knowing this?? 2)and... (2 Replies)
Discussion started by: aish11
2 Replies

6. Solaris

RBAC related question.....

I am referring Bill Calkins(SCSA exam prep) for RBAC..actually i wanted to make a normal user to get the privilege to run a command through authorization, not through profile files... This is the exact steps given by Bill calkins.. 1.roleadd -m -d /export/home/adminusr -c... (11 Replies)
Discussion started by: saagar
11 Replies

7. UNIX for Advanced & Expert Users

One Question related to alias

Hello, I have created following alias in csh lab 'rlogin -l user23 complab23' but problem is complab23 does not allow automatic login by checking .rhosts file. So after typing lab on command line I have to type complicate password and if wrong password is typed thrice then account gets... (4 Replies)
Discussion started by: neerajrathi2
4 Replies

8. Programming

signals related question

Hi all, Just a little question relative to signals. I know that if an application is in the sleep state, When a signal is catched, it will be processed by the handler. But what happens if it's processing something? Does the processing stops?? The following code should illustrate this case ... (2 Replies)
Discussion started by: ninjanesto
2 Replies

9. Shell Programming and Scripting

a math related question

hello: First I know the rules on Homework related questions, I wrote my script, but I cannot seem to figure out how to do one math problem. How do I take a zip code and seperate the idvidual digits? I used the modulus expression and divided the number by 10 ^ n but that only worked... (9 Replies)
Discussion started by: jahjah
9 Replies

10. UNIX for Dummies Questions & Answers

A Question related to the net

well, I was suggested to remove the contents of the cache as i get out of the browser netscape from the .netscape folder. is that really necessary? if so what are the rest to be done? can anybody please tell me?:rolleyes: (8 Replies)
Discussion started by: sskb
8 Replies

Featured Tech Videos