1845948 Members
2770 Online
110250 Solutions
New Discussion

Re: Shell Script Help

 
SOLVED
Go to solution
Steven Chen_1
Super Advisor

Shell Script Help

I usually have one process preventing Oracle DevpSuite to restart all over. I am thinking a process killing would do, but I need to get the first line of pid, which I need help.

My script:

ToStop=`ps -ef|grep '/disk1/ora'|awk '{ print $2 }
cat $ToStop| (need help here)
kill -9 (first line of pid)

The hung PID of $ToStop would be on the top, i.e.

7388
7489
9999

How can I just get 7388 to be "kill -9 ..."?

Thanks,

Steven



Steve
7 REPLIES 7
Geoff Wild
Honored Contributor
Solution

Re: Shell Script Help

pipe to head -1

kill -9 `cat $ToStop | head -1`

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
renarios
Trusted Contributor

Re: Shell Script Help

Hi Steven,

Is this what you want?

for process in $( ps -ef|grep '/disk1/ora'|grep -v grep|grep -v kill_ | awk '{print $2}' )
do
kill ${process}
done

Hope it helps,

Renarios
Nothing is more successfull as failure
RAC_1
Honored Contributor

Re: Shell Script Help

Do not use ps -ef|grep grep stuff.
Rather use UNIX95 variable.

pid=$(UNIX95= ps -C"command/application name" -o "pid" | grep -v PID)

Then kill it. Also do not us kill -9 directly, it is not good. I start with default (-15), then 1, 2, 3 and 11. IF nothing works then last resort -9
-11 is equally effective and does cleanup work which -9 does not do.

Anil


There is no substitute to HARDWORK
renarios
Trusted Contributor

Re: Shell Script Help

Hi RAC,

I can't find UNIX95 on my HP-UX system, so I will do it with grep. Kill -9 does not kill child processes, so use default kill command.

Cheers,

Renarios
Nothing is more successfull as failure
Peter Godron
Honored Contributor

Re: Shell Script Help

Steven,
UNIX95 is explained here:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=4177
The UNIX95 solution is very neat in my opinion.
Regards
Bill Hassell
Honored Contributor

Re: Shell Script Help

The UNIX95= construct is simply defining a variable temporarily (it could read: UNIX95=1) which ps will then read in it's environment and enable 3 VERY useful options: -C -H and -o. The option you need is to avoid at all costs the use of grep and ps. The reason is that grep will NOT look where you want! Instead, it looks across the entire line, so if the process is called bill, and there are also users called bill, billy and wildbill on your system, *ALL* of those user process will be found with grep (and be killed in your script). Not good.

In your example, you are grep'ing for /disk1/ora...and this looks like a directory rather than a process called ora. This is a really bad idea if there are other porceses in the /disk1/ora directory. And depending on how your problem process was started, the text: /disk1/ora may not even appear on the command line.

So, always use the UNIX95 option with ps. DO NOT globally set UNIX95 since it affects other programs and libraries--simply set it temporarily as in the example (yes, it's OK to set a variable in front of a command), or even better, alias ps like this:

alias ps="UNIX95= /usr/bin/ps"

and now, all your ps commands will have the extra options available.

Now let's assume that your process is called myprog. To find *exactly* myprog and not 21myprog or myprogram:

UNIX95= ps -f -C myprog

You can also use another special option of ps (enabled by UNIX95) called -o which lets you define the fields to display:

UNIX95= ps -C myprog -o pid,args

Now you have just a list of PID plus the command line.

Now for the sysadmin lecture #2: NEVER USE kill -9

The reason is that this kill destroys the process, it leaves shared memory segments orphaned causing memory leaks, it leaves data in RAM not properly posted to disk, in short, it leaves a mess with complex programs, especially database programs. Using kill -9 should be the very last resort. If you kill Oracle processes this way, yoour DBA had better run a health check on the database because there likely broken chains and invalid links scattered around the database.

So you always use kill -15 which is also know as signal SIGTERM. Most sophisticated programs will trap this signal and perform an orderly shutdown. If this doesn't work, then you can try a more significant signal like kill -1, also known as SIGHUP. This signal is generated when the connection "hangs up" as in a modem connection. If the process still does not respond (by terminating), then you ask the programmer why the process is not working according to good programming standards.

If all else fails, then kill -9 should work but you need to investigate whether this drastic step has caused problems with the files that this program uses.

So to rewrite your script, you ALWAYS assume the worst, that you'll get no results, or that you'll get multiple results when you're expecting 1 and only one process. Something like this:

MYPROC=myprog
PIDLIST=$(UNIX95= ps -C $MYPROC -o pid | grep -v PID)
if [ $(echo $PIDLIST | wc -w) -lt 1 ]
then
echo $MYPROC is not running
exit 0
fi

if [ $(echo $PIDLIST | wc -w) -gt 1 ]
then
echo "$MYPROC has multiple copies running:"
ps -f -p ps -f -p $(echo $PIDLIST|tr -s " " ",")
exit 1
fi

kill -15 $PIDLIST
if [ $(ps -p $PIDLIST > /dev/null) ]
then
kill -1 $PIDLIST
if [ $(ps -p $PIDLIST > /dev/null) ]
then
echo "$MYPROC (PID=$PID) will not terminate"
ps -f -p $PIDLIST
exit 2
fi
fi

In the above code, it finds all the PIDs that have the name $MYPROC, then exit normally if there are none, exit 1 if there is more than 1 (exit code = 1) and finally, kill the process with -15, and if that fails, kill -1, and if that fails put out a warning.

Automated kill scripts are a great way to cause a lot of problems, thus all the extra code for reliability. A good script writer always assume that everything will fail.


Bill Hassell, sysadmin
renarios
Trusted Contributor

Re: Shell Script Help

Thanks Bill,

That's a good explanation and darned handy to use.

Cheers,

Renarios
Nothing is more successfull as failure