- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell Script Help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 01:23 AM
04-12-2005 01:23 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 01:29 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 01:30 AM
04-12-2005 01:30 AM
Re: Shell Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 01:30 AM
04-12-2005 01:30 AM
Re: Shell Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 01:41 AM
04-12-2005 01:41 AM
Re: Shell Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 02:08 AM
04-12-2005 02:08 AM
Re: Shell Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 05:30 AM
04-12-2005 05:30 AM
Re: Shell Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2005 08:38 PM
04-12-2005 08:38 PM
Re: Shell Script Help
That's a good explanation and darned handy to use.
Cheers,
Renarios