<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Killing a process in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981198#M95660</link>
    <description>Hey gang,&lt;BR /&gt;&lt;BR /&gt;OK...first of all, be gentle.  You know how bad my script writing skills are.  OK...here goes...&lt;BR /&gt;&lt;BR /&gt;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 &lt;THING i=""&gt;") and I need something that will kill ALL of the processes that return from the ps.&lt;BR /&gt;&lt;BR /&gt;Does anyone have any nice neat solutions for these two situations?  Thanks again!!!&lt;/THING&gt;</description>
    <pubDate>Fri, 13 Apr 2007 19:41:24 GMT</pubDate>
    <dc:creator>TheJuiceman</dc:creator>
    <dc:date>2007-04-13T19:41:24Z</dc:date>
    <item>
      <title>Killing a process</title>
      <link>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981198#M95660</link>
      <description>Hey gang,&lt;BR /&gt;&lt;BR /&gt;OK...first of all, be gentle.  You know how bad my script writing skills are.  OK...here goes...&lt;BR /&gt;&lt;BR /&gt;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 &lt;THING i=""&gt;") and I need something that will kill ALL of the processes that return from the ps.&lt;BR /&gt;&lt;BR /&gt;Does anyone have any nice neat solutions for these two situations?  Thanks again!!!&lt;/THING&gt;</description>
      <pubDate>Fri, 13 Apr 2007 19:41:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981198#M95660</guid>
      <dc:creator>TheJuiceman</dc:creator>
      <dc:date>2007-04-13T19:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: Killing a process</title>
      <link>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981199#M95661</link>
      <description>First of all grep is a terrible tool to match processes; use the XPG4 ps behavior.&lt;BR /&gt;&lt;BR /&gt;Now:&lt;BR /&gt;COMMAND="myproc" # you will probably pass this as a parameter&lt;BR /&gt;UNIX95=1 ps -C "${COMMAND}" -o pid='' | while read P&lt;BR /&gt;  do&lt;BR /&gt;     echo "PID = ${P}"&lt;BR /&gt;     echo "Kill goes here but if I see a kill -9 you are dead"&lt;BR /&gt;     echo "Start with kill -15 then -3 then -11 and then only if absolutely required kill -9"&lt;BR /&gt;     # test to see if PID is still valid&lt;BR /&gt;     kill -0 ${P}&lt;BR /&gt;     STAT=${?}&lt;BR /&gt;     if [[ ${STAT} -eq 0 ]]&lt;BR /&gt;       then&lt;BR /&gt;         echo "Process ${P} is still alive"&lt;BR /&gt;       fi&lt;BR /&gt;&lt;BR /&gt;  done&lt;BR /&gt;--------------------------------&lt;BR /&gt;This should be more than sufficient for you to get a script working.</description>
      <pubDate>Fri, 13 Apr 2007 20:13:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981199#M95661</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2007-04-13T20:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Killing a process</title>
      <link>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981200#M95662</link>
      <description>kill $(UNIX95= ps -C &lt;PROCESS&gt; -o pid=)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;so for example&lt;BR /&gt;&lt;BR /&gt;kill $(UNIX95= ps -C java -o pid=)&lt;BR /&gt;&lt;BR /&gt;and that should kill any java process running&lt;/PROCESS&gt;</description>
      <pubDate>Fri, 13 Apr 2007 20:20:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981200#M95662</guid>
      <dc:creator>Aussan</dc:creator>
      <dc:date>2007-04-13T20:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Killing a process</title>
      <link>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981201#M95663</link>
      <description>Repeat the mantra:&lt;BR /&gt; &lt;BR /&gt;ps and grep are not reliable!!!!&lt;BR /&gt; &lt;BR /&gt;And the affirmation:&lt;BR /&gt; &lt;BR /&gt;ps can find exactly what you want.&lt;BR /&gt; &lt;BR /&gt;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:&lt;BR /&gt; &lt;BR /&gt;-u find all processes owned by a user &lt;BR /&gt;-p find a specific process by PID&lt;BR /&gt;-C find a process by name&lt;BR /&gt; &lt;BR /&gt;There are a lot more but these are commonly overlooked. NOTE: these are *exact matches, not accidental matches to the wrong field. Consider:&lt;BR /&gt; &lt;BR /&gt;Looking for process 123&lt;BR /&gt;ps -ef | grep 123&lt;BR /&gt;...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.&lt;BR /&gt; &lt;BR /&gt;-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:&lt;BR /&gt; &lt;BR /&gt;UNIX95=1 ps -f -C sh&lt;BR /&gt; &lt;BR /&gt;and now only sh processes will be found. Compare that to:&lt;BR /&gt; &lt;BR /&gt;ps -ef | grep sh&lt;BR /&gt; &lt;BR /&gt;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:&lt;BR /&gt; &lt;BR /&gt;UNIX95=1 ps -C sh -o pid,ruser,arg&lt;BR /&gt; &lt;BR /&gt;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:&lt;BR /&gt; &lt;BR /&gt;Simplest:&lt;BR /&gt;K=$(UNIX95=1 ps -C myprog -o pid=)&lt;BR /&gt;kill -15 $K&lt;BR /&gt; &lt;BR /&gt;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.&lt;BR /&gt; &lt;BR /&gt;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.</description>
      <pubDate>Fri, 13 Apr 2007 20:39:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/killing-a-process/m-p/3981201#M95663</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2007-04-13T20:39:48Z</dc:date>
    </item>
  </channel>
</rss>

