- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Killing a process
Operating System - Linux
1752772
Members
4974
Online
108789
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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-13-2007 12:41 PM
тАО04-13-2007 12:41 PM
Hey gang,
OK...first of all, be gentle. You know how bad my script writing skills are. OK...here goes...
I need to write a script that will go out and find a process (a ps) and then kill it. I need something that will kill just a particular process (one particular return from a 'ps' command grepping out for a particular thing like "ps -ef|grep") and I need something that will kill ALL of the processes that return from the ps.
Does anyone have any nice neat solutions for these two situations? Thanks again!!!
OK...first of all, be gentle. You know how bad my script writing skills are. OK...here goes...
I need to write a script that will go out and find a process (a ps) and then kill it. I need something that will kill just a particular process (one particular return from a 'ps' command grepping out for a particular thing like "ps -ef|grep
Does anyone have any nice neat solutions for these two situations? Thanks again!!!
Solved! Go to Solution.
- Tags:
- kill
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-13-2007 01:13 PM
тАО04-13-2007 01:13 PM
Re: Killing a process
First of all grep is a terrible tool to match processes; use the XPG4 ps behavior.
Now:
COMMAND="myproc" # you will probably pass this as a parameter
UNIX95=1 ps -C "${COMMAND}" -o pid='' | while read P
do
echo "PID = ${P}"
echo "Kill goes here but if I see a kill -9 you are dead"
echo "Start with kill -15 then -3 then -11 and then only if absolutely required kill -9"
# test to see if PID is still valid
kill -0 ${P}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Process ${P} is still alive"
fi
done
--------------------------------
This should be more than sufficient for you to get a script working.
Now:
COMMAND="myproc" # you will probably pass this as a parameter
UNIX95=1 ps -C "${COMMAND}" -o pid='' | while read P
do
echo "PID = ${P}"
echo "Kill goes here but if I see a kill -9 you are dead"
echo "Start with kill -15 then -3 then -11 and then only if absolutely required kill -9"
# test to see if PID is still valid
kill -0 ${P}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Process ${P} is still alive"
fi
done
--------------------------------
This should be more than sufficient for you to get a script working.
If it ain't broke, I can fix that.
- Tags:
- UNIX95
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-13-2007 01:20 PM
тАО04-13-2007 01:20 PM
Re: Killing a process
kill $(UNIX95= ps -C -o pid=)
so for example
kill $(UNIX95= ps -C java -o pid=)
and that should kill any java process running
so for example
kill $(UNIX95= ps -C java -o pid=)
and that should kill any java process running
The tongue weighs practically nothing, but so few people can hold it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-13-2007 01:39 PM
тАО04-13-2007 01:39 PM
Solution
Repeat the mantra:
ps and grep are not reliable!!!!
And the affirmation:
ps can find exactly what you want.
Seriously, combining grep and ps is always a disaster because you can't tell grep where to search. So start by NEVER combining grep and ps in a script and learning the many built-in search features of grep:
-u find all processes owned by a user
-p find a specific process by PID
-C find a process by name
There are a lot more but these are commonly overlooked. NOTE: these are *exact matches, not accidental matches to the wrong field. Consider:
Looking for process 123
ps -ef | grep 123
...finds bill123 (a user ID on the system) and also finds 1123 as well as 11234 and of course, and process with a command line containing 123. But ps -f -p 123 will find exactly one process -- no mistakes.
-C process_name needs some details. This is an exact match so while the processes sh ksh bash csh unhashdaemon are all the same to grep sh, -C sh is an exact match. However, -C (and -o) are part of the XPG4 implementation so a flag is needed to enable the options. This is the method:
UNIX95=1 ps -f -C sh
and now only sh processes will be found. Compare that to:
ps -ef | grep sh
Now for your task: You first must define the criteria for the processes to be found. It may be by user (-u) or by name (-C) or perhaps by some item on the command line. Then use the DIY option -o (Do-It-Yourself) to define the fields and order you want them. So you pick thie fields you want to see. For instance:
UNIX95=1 ps -C sh -o pid,ruser,arg
and you see just the 3 columns listed. Now for your kill script, once you have extensively tested the criteria, you can create the kill list a number of different ways:
Simplest:
K=$(UNIX95=1 ps -C myprog -o pid=)
kill -15 $K
Whatever you do, don't use kill -9. And you must protect the kill list from killing critical kernel processes. The best way is to run your script as an ordinary user.
And don't be tempted to set UNIX95 in your environment. It changes the behavior of a number of libraries and other prorams. Use it on the command line so it disappears automatically.
Bill Hassell, sysadmin
ps and grep are not reliable!!!!
And the affirmation:
ps can find exactly what you want.
Seriously, combining grep and ps is always a disaster because you can't tell grep where to search. So start by NEVER combining grep and ps in a script and learning the many built-in search features of grep:
-u find all processes owned by a user
-p find a specific process by PID
-C find a process by name
There are a lot more but these are commonly overlooked. NOTE: these are *exact matches, not accidental matches to the wrong field. Consider:
Looking for process 123
ps -ef | grep 123
...finds bill123 (a user ID on the system) and also finds 1123 as well as 11234 and of course, and process with a command line containing 123. But ps -f -p 123 will find exactly one process -- no mistakes.
-C process_name needs some details. This is an exact match so while the processes sh ksh bash csh unhashdaemon are all the same to grep sh, -C sh is an exact match. However, -C (and -o) are part of the XPG4 implementation so a flag is needed to enable the options. This is the method:
UNIX95=1 ps -f -C sh
and now only sh processes will be found. Compare that to:
ps -ef | grep sh
Now for your task: You first must define the criteria for the processes to be found. It may be by user (-u) or by name (-C) or perhaps by some item on the command line. Then use the DIY option -o (Do-It-Yourself) to define the fields and order you want them. So you pick thie fields you want to see. For instance:
UNIX95=1 ps -C sh -o pid,ruser,arg
and you see just the 3 columns listed. Now for your kill script, once you have extensively tested the criteria, you can create the kill list a number of different ways:
Simplest:
K=$(UNIX95=1 ps -C myprog -o pid=)
kill -15 $K
Whatever you do, don't use kill -9. And you must protect the kill list from killing critical kernel processes. The best way is to run your script as an ordinary user.
And don't be tempted to set UNIX95 in your environment. It changes the behavior of a number of libraries and other prorams. Use it on the command line so it disappears automatically.
Bill Hassell, sysadmin
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP