Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
parentPid=$1
echo `ps -ef | grep "\ ${parentPid}\ " | grep -v grep 2>/dev/null`

This command is actually part of a function to stop a server.This is a already existing command in a .sh file and it is executing fine I just want to know what will this command do, as I am not understanding. I want to know what does grep within a grep do.

Can anybody explain me this command?

share|improve this question
    
What did you try so far and where do you run into problems? parentPid=$1 depends on the shell you are using (E.g. dash? zsh? sh? pinosh?) In my test case the result of that was this: >set parentPid=$1 >echo $parentPid > (Nothing). Back at the shell prompt. Which leads me to suspect that you might want to run both of them from the same script, or maybe concattenated with ;, && or ||. However those are not in the OP. Just a newline and a extra whitespace in front of the second command. –  Hennes Oct 14 '13 at 14:11
    
The next command is ps From man ps: The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals. -e Display the environment as well. -f Show commandline and environment information about swapped out processes. This option is honored only if the UID of the user is 0. Which does different thing depe4nding on weather you are running as uid 0 or not. Please add how you are running this to the post as well. –  Hennes Oct 14 '13 at 14:12
    
actually i want to know what exactly the second line will do. –  Rosylin Oct 14 '13 at 14:16
    
ps -ef | grep "\ ${parentPid}\ " | grep -v grep 2>/dev/null –  Rosylin Oct 14 '13 at 14:17
    
You might want to add that to your original post, using the edit button. In the current state the post simply does not have enough information and it shows no research effort. That is reason for closure. Please add more information. Add what you tried. Add where you run into problems (e.g. the filtering out of grep itself near the end of the second set of chainned commands, or earlier, or the redirect of std_error to dev null, ... –  Hennes Oct 14 '13 at 14:18
add comment

1 Answer

The command searches, among the normally very large output of ps, for a single line, that which concerns the process identified by the number passed to a shell script as the first argument after the shell name invocation.

ps -ef is a standard invocation: it shows all processes (-e option) in full format (-f). The large output is passed to the first invocation of grep, which locates all lines containing a process identification. If you had tried the command in its shortened form,

echo `ps -ef | grep "\ ${parentPid}\ " 

you would have noticed that there are two lines containing this process ID: one is the line we are interested in, with process owner, CPU sage, and so on. The other line is the command "grep "\ ${parentPid}\ " itself, where however the expression within "" has been replaced by the shell with the value of the variable. Of course, this line is of no interest to us, because it is nothing but the command we typed in. So the second invocation of grep says: remove (option -v) from these two lines the one which contains the word grep (and, in passing, toss any message error into the abyss, = 2>/dev/null).

Thus, this second invocation of grep removes from the output the useless process that comes from

grep "\ {parentPid}\ "

and leaves, as the ONLY line of output, the one concerning the process $1 = parentPid. That's all, folks.

share|improve this answer
    
Thanks for your explanation. I have understood it. –  Rosylin Oct 15 '13 at 7:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.