- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Killing a process within a script
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
Discussions
Discussions
Forums
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
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
тАО02-17-2006 01:32 PM
тАО02-17-2006 01:32 PM
What would be the easiest way to kill a process within a script? I'm wanting to perform a ps on something and then kill that process through automation. Thanks for your help.
Solved! Go to Solution.
- Tags:
- kill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2006 11:49 PM
тАО02-17-2006 11:49 PM
Re: Killing a process within a script
I would do something like
# kill -9 `ps -ef | grep PROCESSNAME | grep -v grep | awk '{print $2}'`
Pablo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2006 12:04 AM
тАО02-18-2006 12:04 AM
SolutionFor the simple opne-liners as per above, I prefer a trivial regexpr versus the 'grep -v grep'
Something like: ps -ef | grep [P]ROCESSNAME
Here the grepped string no longer matches itself as [P] really is just P.
And if one goes into awk anyway, why not have it do it all? It gives the oppurtunity for additional tests, log messages, whatever...
To pick up a variabled (processname?) into awk, check the manpages. Notably: -v and x=y and the ENVIRON[] built-in array.
ps -ef | awk '/PROCESSNAME/ { system ("kill -9 " $2)}'
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2006 12:07 AM
тАО02-18-2006 12:07 AM
Re: Killing a process within a script
First, make usre that you isolate the process that you *really* want. Use the XPG4 (UNIX95) options of 'ps' achieves this.
Never kill with 'kill -9' except as a last resort. A 'kill -9' cannot be ignored or trapped and it doesn't give a process any chance to clean up shared memory segments or remove temporary files.
Instead, use a multi-level kill, starting with a hangup; then a simple 'kill -15'; and as a last resort a 'kill -9'.
The following code accomplishs the above. If any level of killing succeeds, the script exits.
# cat .killer
#!/usr/bin/sh
myproc=`basename ${1}`
mypid=`UNIX95= ps -C ${myproc} -o pid=`
if [ ! -z "${mypid}" ]; then
kill -1 ${mypid} 2>/dev/null || exit 0
sleep 3
kill -15 ${mypid} 2>/dev/null || exit 0
sleep 3
kill -9 ${mypid} 2>/dev/null
fi
exit 0
You can see how this works by doing:
# sleep 120 &
# ./killer sleep
(or)
# nohup /usr/bin/sleep 120 &
# ./killer sleep
If you wish to integrate this into a subroutine in an existing script, change the 'exit' to a 'return' within the subroutine.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2006 12:42 AM
тАО02-18-2006 12:42 AM
Re: Killing a process within a script
# ps -efx | grep/awk... #(>= hp-ux 11.11)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2006 01:32 AM
тАО02-18-2006 01:32 AM
Re: Killing a process within a script
If there is any chance that you have multiple processes by the same name, the version below accomodates that. The first version could potentially fail to kill all of the processes under certain circumstances. The same comments apply as before, otherwise.
# cat .killer
#!/usr/bin/sh
myproc=`basename ${1}`
[ -z "${1}" ] && { echo "no process specified!"; exit 1; }
mypid=`UNIX95= ps -C ${myproc} -o pid=`
if [ ! -z "${mypid}" ]; then
kill -1 ${mypid} 2>/dev/null
sleep 3
kill -15 ${mypid} 2>/dev/null
sleep 3
kill -9 ${mypid} 2>/dev/null
fi
exit 0
Regards!
...JRF...
ENOCOFFEE ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2006 02:34 AM
тАО02-18-2006 02:34 AM
Re: Killing a process within a script
I do appreciate James' script,
I'm sorry for my sudden "kill -9", I was really rude.
I still have a point:
Let's suppose you have 2 processes:
# sleep 1000 &
# sleep 1001 &
and you wanna stop *only* "sleep 1000"
(java processes happen to have long parameters and I find useful the "ps -x" trick)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2006 08:13 PM
тАО02-19-2006 08:13 PM
Re: Killing a process within a script
in this case use:
kill $(ps -fxu
trick is -x option to be able to see by PS the while command string so you will be abel to check command and parameter as well.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2006 12:57 AM
тАО02-20-2006 12:57 AM
Re: Killing a process within a script
sudo -u root kill -9 `awk '{print $2}' temp.1`
rm temp.1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-21-2006 09:39 AM
тАО02-21-2006 09:39 AM