A question

Hi,
I'm new to unix and got struck here.Can any one help me out.My question is ..
is the command

if [ ps -ef |grep "any pid"];
then
  echo "do some stuff"
fi 

correct?

Thanks in advance
abhijeet

ps -ef | grep -q "any pid"
if [ $? -eq 0 ]
then
    echo "do some stuff"
fi

Thanks Balajesuri!! :slight_smile:

Can you throw some light on the syntax too.. it would be helpfull.

ps -ef | grep -q "any pid" => Search for "any pid" from the output of ps -ef but don't display any output (-q takes care of this).
if [ $? -eq 0 ]            => $? is the exit status of last command executed. $? is set to 0 if the last command executed is a success.

suppose i want to grep only that PIDs which have its parrent pid as 1, then what should i do?

Something similar...

if  pgrep "some_pid" >/dev/null
then 
  echo "do some stuff"
fi

pgrep = process id grep

--ahamed

2 Likes

Thanks Ahamed!!

can you help me little more.. :slight_smile:
suppose we have a pid= 15641 and it PPID =1
i want to check if this process is there i'll do some stuff..
now how would i check

Didn't quite get you... You want to check for PPID also?...

--ahamed

yes i want to checka process with PID and its PPID too
Greping it twice using Pipe will work??

I imagine a great many things will have a ppid of 1... why check it? If you have the PID, that's enough.

There will be only one process with a particular PID whose PPID is 1 right?
I want to see that process only..

What Operating System and version do you have? There is much variation in the "ps" command and "pgrep" is only available on a few systems.

I'm using Solaris, version i'll let you know tomorrow...
can you guide me some "ps" or "pgrep" command that will help me getting the process which has particular PID and PPID =1.

---------- Post updated 02-08-12 at 12:26 AM ---------- Previous update was 02-07-12 at 10:49 PM ----------

I'm breaking my head on it..
correct me if i'm wrong
there will be one and only one process running which have particular PID say '12345' and having PPID as 1.. right?

I want to grep this process.which argument should i use with ps or pgrep command

Regards

I don't have your Solaris to try.
A quick fix is to use ordinary grep.

I my "ps" there are exactly 5 space characters after PID and before the start of PPID. Note the leading space before "1085" and the trailing space after "1" to avoid accidental matches. The "1085" was just a PID I was trying.

ps -ef|grep " 1085     1 "|grep -v "grep"

Afterthought:
This would be much more efficient as:

ps -fp1085 | grep " 1085     1 "|grep -v "grep"

Couldn't find a suitable "pgrep" command.

Thanks a lot!! :slight_smile:

There will only be one process with a certain PID. The PPID doesn't really narrow any further.

Thanks Methyl/Ahamed/Corona/Balajesyri !!
i appreciate your help.. these commands worked fine.. got what i was looking for :slight_smile:
dont mind if i bug you more :stuck_out_tongue:

---------- Post updated at 12:33 PM ---------- Previous update was at 12:27 PM ----------

@Corona

You are right .. there will be only one process with a certain PID.. but that process could have some child process and if grep for PID.. all will be listed,that is the process itself and the child process of that process..
and if you grep it along with PPID you will get the desired result.. thats what i was looking for :slight_smile:

ps -ef | grep "$pid[ ]*$ppid"

Sorry for the late response. You already got the reply. I have started to love AWK and trying to learn the same. So can we do something like this:

ps -ef | awk '{if ($2 == 4063 && $3 == 1) {print $0}}' # where $2 is for PID field and $3 is PPID field
 
# Output:
user      4063     1  0 Xxxxx ?        00:00:00 process_name

(I have hidden some values for obvious reasons. It may be LAME still :smiley: )