- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Killing a process by command name
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
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
12-01-2005 04:38 AM
12-01-2005 04:38 AM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2005 04:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2005 05:01 AM
12-01-2005 05:01 AM
Re: Killing a process by command name
To do this safely do this:
# TASK=/usr/bin/sleep #...for example, only!
# PIDS=`UNIX95= ps -C ${TASK##*/} -o pid='`
if [ ! -z "${PIDS}" ]; then
kill -1 ${PIDS} > /dev/null 2>&1
sleep 3
kill -15 ${PIDS} > /dev/null 2>&1
sleep 3
kill -9 ${PIDS} > /dev/null 2>&1
fi
The UNIX95 variation with the '-C' switch finds *only* the prcoess with a basename that matches its argument.
The three-level kill gives the process a chance to catch the signal and cleanup temporary files and/or shared memory segements.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2005 05:25 AM
12-01-2005 05:25 AM
Re: Killing a process by command name
You can do simply with this :
# PID= $(ps -ef | grep "command" | grep -v grep | awk '{print $2}') > kill.list
#
for i in `cat kill.list`
do
kill -SIGHUP $i
echo "sent SIGHUP to PID = $i ."
sleep 1
kill -9 $i
echo "sent SIGKILL to PID = $i ."
done
You can include in a script and put that in a cron to execute as per need.
Hth,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2005 05:28 AM
12-01-2005 05:28 AM
Re: Killing a process by command name
instead of PID= its :
# ps -ef | grep "command" | grep -v grep | awk '{print $2}' > kill.list
Cheers,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2005 05:45 AM
12-01-2005 05:45 AM
Re: Killing a process by command name
#!/usr/bin/sh
typeset TARGET=${1} # process to kill
shift
typeset -i PID=0
typeset PROC=''
ps -e | awk '{print $1,$NF}' | while read PID PROC
do
if [[ "${PROC}" = "${TARGET}" ]]
then
kill -15 ${PID}
fi
done
I am assuming that the process you are not trying kill is awk; if so then additional logic will be needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2005 11:36 AM
12-01-2005 11:36 AM
Re: Killing a process by command name
ps has a very important but overlooked option: -C proc_name. It is only valid when the temporary variable UNIX95 is set. Compare the output of these two commands:
ps -ef | grep sh
UNIX95=1 ps -f -C sh
So always use ps (not grep or awk or other pattern matching) to find the exact name of a process. You can have multiple -C options to search for several process names.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2005 08:23 PM
12-01-2005 08:23 PM
Re: Killing a process by command name
for PidNbr in $(ps -ef|awk '$1=="user"&&$8="command" {print $2}')
do
kill $PidNbr
done
for PidNbr in $(ps -ef|awk '$1=="user"&&$8="command" {print $2}')
do
kill -9 $PidNbr
done
where user is the UNIX user if the command, and command is the comamnd you want to kill.
HTH,
Art