1825139 Members
4980 Online
109679 Solutions
New Discussion юеВ

Re: ps -ef command

 
SOLVED
Go to solution
Scott E Smith
Frequent Advisor

ps -ef command

I need the command options to get a list of running processes that have a PPID of 1 (root).
I have an appliction that orphans it's processes to root and I amd workling on a script to clean them up. Any ideas/ Thanks.
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: ps -ef command

Hi,

The simplest way I could think of was this:

#!/usr/bin/sh

ps -ef | while read UID PID PPID DUMMY
do
if [ "${PPID}" = "1" ]
then
ps -p ${PID} -f
fi
done

Regards, Clay
If it ain't broke, I can fix that.
Uday_S_Ankolekar
Honored Contributor

Re: ps -ef command


Hi,

ps -ef | grep $process | grep -v grep | cut -c 10-14,15-20,39-44 | tr -s " "

This will give outpur of all runaway process.
$process should be replaced by the process name

Good Luck
-USA..
Good Luck..
James R. Ferguson
Acclaimed Contributor

Re: ps -ef command

Hi Scott:

# PIDS=`ps -el|awk '$3 > 100 && $5 = 1 && $12 ~/\?/ {print $4}'`

...will give you a list of candidates. Field-3 is the 'uid' for which you want to return only "safe" *user* processes. Since I start "normal" user definitions in /etc/passwd at 100 and above, I have shown this case. Field-5 is the 'ppid' you seek; and field-4 is the 'pid' you might kill.

If you are going to kill processes, do so with a simple (-15) kill and/or a hangup signal (kill -1) rather than a 'kill -9'. This gives processes a chance to cleanup temporarary files, shared memory and otherwise terminate gracefully.

The capture of your candidate list in a variable like "PIDS" allows you to issue a 'kill -1' for instance; wait 5-10 seconds; and then (if you must), issue a 'kill -9' using the variable list again.

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: ps -ef command

Try this way,

UNIX95= ps -e -o "ppid args"

You can do anything with the output.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Roger Baptiste
Honored Contributor

Re: ps -ef command

hi,

Try the following command:

UNIX95= ps -e -o user,pid,ppid,comm |sort -nk3 | more

This will list the processes with the PPID
in the ascending order.

HTH
raj
Take it easy.
Sridhar Bhaskarla
Honored Contributor

Re: ps -ef command

I guess I should be more specific.

To find out all the processes that has the PPID of 1, use awk to filter the processes

UNIX95= ps -e -o "ppid args" |awk '$1 = 1 {print $2}'


To verify the above output try the same replacing $2 with $0.

BTW UNIX95 gives XPG4 behaviour of PS that is very handy.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Kevin Wright
Honored Contributor

Re: ps -ef command

export KILL_PID=`ps -ef | grep -v root | awk '{ if ( $3 -eq
1 ) print $1" "$2 }' `

then you can test with
if [[ ! -z $KILL_PID ]];then
......
.....

then to kill you could do..
export TOKILL=`echo $KILL_PID |awk '{print $2}'
kill -9 $TOKILL
A. Clay Stephenson
Acclaimed Contributor

Re: ps -ef command

Hi Scott:

Whatever you do, don't kill these guys with a kill -9 - that is almost always a BAD idea because no cleanup is done (Remove temp files, shared memory, etc.)

I would write a function to kill these guys
with a kill -15, kill -1, kill -2, kill -11 in that order. You can test with a kill -0 to see if the process is still around. kill -11 is almost as sure a kill as kill -9 and does cleanup. It you want to add kill -9 to the end of that list - well that's up to you.

If it ain't broke, I can fix that.