jQuery in Action, 2nd edition*
The moose likes Linux / UNIX and the fly likes How to Kill Process in Unix by using name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to Kill Process in Unix by using name" Watch "How to Kill Process in Unix by using name" New topic
Author

How to Kill Process in Unix by using name

Mahesh Dwarapureddi
Greenhorn

Joined: Mar 27, 2014
Posts: 5
Hi,

Am using the below two command to kill the process by using name

>Killall -f gwr
getting: pkill:12062 - operation not permitted
>Killall -9 gwr
getting: gwr: no process killed
>killbyname gwr
getting: -bash:killbyname: command not found


gwr is the process name that want to kill

Could you please suggest is there any other command or way to kill the process by using its name

Roger Sterling
Ranch Hand

Joined: Apr 06, 2012
Posts: 426

Why do you not want to use the PID ?
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

I presume you want to use the process name because you don't have it's PID. In which case you can use 'ps' to list the processes then pipe it's output to grep looking for the name of the process then pipe that output to awk to extract the PID and then kill that PID. Something like

to kill VirtualBox.

Please note that this is not that rugged and I'm betting there is a much much better way of doing this.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11447
    
  16

Which shell are you using?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mahesh Dwarapureddi
Greenhorn

Joined: Mar 27, 2014
Posts: 5
Because am running in automtion suite for some requirements by using shell script. Automation.sh

That script will start the gwr server, at the end it should automatically kills the process. That is the reason I should not use PID, because every time PID get changed for each run

Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Mahesh Dwarapureddi wrote:Because am running in automtion suite for some requirements by using shell script. Automation.sh

That script will start the gwr server, at the end it should automatically kills the process. That is the reason I should not use PID, because every time PID get changed for each run



A standard approach to this is to record the PID of the process (writing it to a file if necessary) when it is started and then use it to kill the process later. Alternatively use the approach I suggested earlier!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11447
    
  16

again - what shell are you using? Some shells have commands that let you kill a process by name, some don't (if i recall correctly).

But killing a process by name is dangerous. What if there are two running simultaneously? How do you know which to kill? do you want to kill both, every time?
Mahesh Dwarapureddi
Greenhorn

Joined: Mar 27, 2014
Posts: 5
am using ubuntu,
Yes I want to kill process every time for all process running with the name gwr.

I wanted to kill whatever process running with gwr name. that is my requirement.

Please suggest is there any other ways.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

I see no point in suggesting other ways since you seem to be ignoring my suggestion and ignoring Fred's question.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

Java used to allow killing threads using a simple method.

That capability was deprecated many years ago. They determined that externally terminating a process is "rude". It doesn't take into account what the thread might be currently doing. What it might be in the middle of where abnormal termination could leave critical resources in a state of confusion.

I could say the same thing about programs. If "gwr" is a server, it should have some way to be informed that it needs to collect all its toys, put them neatly away and shut down.

Quite a few Unix/Linux daemon applications do use the "kill" system to initiate shutdown, but they aren't simply brutally cancelled, they have "kill" handlers to intercept something like a SIGHUP and commence shutdown when one is received.

Generally the way that the system knows what the daemon process is is because the process startup records its PID in an agreed-upon place. For example, /var/run/bacula-fd. Thus signalling the server becomes as simple as a

Java processes, such as the Tomcat webapp container do have special issues, since the PID is actually for the JVM as a whole and since Java itself is "write-once/run-anywhere", meaning that there's no real support for OS-specific features like PIDs, it's a little harder to get what you need. But Tomcat does it, so you can see that it is possible. Although in Tomcat's case, the preferred method of shutdown from an external controller is to send a shutdown command to Tomcat's command port (defaults to 8009).


Customer surveys are for companies who didn't pay proper attention to begin with.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

By the way. "State of confusion" is a euphemism that includes "possible security exploit".

Or, at a minimum, hours of fun and antacid consumption as you manually figure out what's damaged and how to fix it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8049
    
  22

Mahesh Dwarapureddi wrote:am using ubuntu,
Yes I want to kill process every time for all process running with the name gwr.
I wanted to kill whatever process running with gwr name. that is my requirement.
Please suggest is there any other ways.

First: Read Tim's post, because it contains very important stuff.

Second: Killing processes is NOT recommended, from Java or any other medium. If you are starting processes that require killing, because they otherwise run forever, then I'd say that you have a problem with your current app or its design. Simply killing stuff sounds like a "band-aid", not a solution, and runs the risk of leaving your system in an inconsistent state (particularly if you use SIGKILL).

Third: (an alternative). Run your gwr processes via a pool. This works similar to a connection pool for databases, but will only work if the processes themselves know when they're done and can hand themselves back to the pool for re-use.

Fourth: (another alternative; but it will probably only work on newer versions of Unix or Linux). Run your gwr processes in a separate VM, and when you're done, shut down the VM. This will ensure that they (or their trees) are stopped according to normal OS practice, as would be done if you shut down the machine itself. If you do it this way, you can also ensure that they don't hog things such as CPU, memory or disk space, which might actually obviate the need for killing them in the first place.

There are many possible ways to do it; and I would strongly suggest that kiiling things should be your LAST option.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

Not sure if the third item above means use "inetd/xinetd" or not. That's a good approach for lightweight servers, but the inetd daemon launches a new copy of the server for every connection, so heavyweight services such as Apache or anything running in a JVM are not a good fit - too much work to start up a new instance over and over again.

Putting something in a VM and terminating the VM doesn't actually shut the server cleanly because the guest OS will do a kill -9 on anything that it cannot persuade to shut itself down. Yes, if you're using a snapshot system image, you won't be left with corrupted disk or RAM, but it's still a brutal solution and it doesn't actually address what other machines in the system would have to do if the server dropped out.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8049
    
  22

Tim Holloway wrote:Not sure if the third item above means use "inetd/xinetd" or not. That's a good approach for lightweight servers, but the inetd daemon launches a new copy of the server for every connection, so heavyweight services such as Apache or anything running in a JVM are not a good fit - too much work to start up a new instance over and over again.

I didn't really have any specific "way" in mind. I was simply thinking of using a pool structure (of which I'm fairly sure there are plenty), and it certainly won't work for a process that never ends by itself.

Putting something in a VM and terminating the VM doesn't actually shut the server cleanly because the guest OS will do a kill -9 on anything that it cannot persuade to shut itself down. Yes, if you're using a snapshot system image, you won't be left with corrupted disk or RAM, but it's still a brutal solution and it doesn't actually address what other machines in the system would have to do if the server dropped out.

But it will do it according to the rules laid out by the shutdown command, which is a tried and tested method for shutting down machines, and which I'm pretty sure (unless things have changed a lot since I was admin-ing) tries a SIGHUP first and allows a certain amount of time for processes to terminate, before doing a SIGKILL. Indeed, it might even try more than one signal type (eg, SIGINT, SIGQUIT...).

However, I think we're both agreed that killing processes from a Java program is probably NOT the best way to proceed.

Winston
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

Winston Gutkowski wrote:
But it will do it according to the rules laid out by the shutdown command, which is a tried and tested method for shutting down machines, and which I'm pretty sure (unless things have changed a lot since I was admin-ing) tries a SIGHUP first and allows a certain amount of time for processes to terminate, before doing a SIGKILL. Indeed, it might even try more than one signal type (eg, SIGINT, SIGQUIT...).

Winston


Problem is, a HUP is no better than a kill -9 if the application hasn't explicitly intercepted and handled it.

It may be that a JVM catches SIGHUP (I haven't checked in a while), but that would only ensure the clean shutdown of the JVM itself at best. The JVM would be forcibly killing the application threads, so the real dirty stuff still applies.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8049
    
  22

Tim Holloway wrote:It may be that a JVM catches SIGHUP (I haven't checked in a while), but that would only ensure the clean shutdown of the JVM itself at best. The JVM would be forcibly killing the application threads, so the real dirty stuff still applies.

Well, there is another possibility (and I may be arguing against myself here): The JVM may not have the power to kill the process. I honestly don't know how it's executed, but it seems unlikely that it would be given straight root access, which is what it would probably need to shut down processes that it doesn't own.

You may well be right, but I still say that killing processes is an admin job, not one that should be done by users - whether via a Java program or not - and if you're relying on a program to "clean up" for you, chances are that there's something wrong with your app.

Winston
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Have I missed something? I see no mention by the OP that his 'gwr' process is Java related.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

There's some confusion here.

I'm not referring to providing a program to kill other programs, I'm referring to the fact that a long-running daemon-type server program shouldn't be using ANY external brute-force method of termination. The target program in question needs to have a clean way to put away all its toys neatly and shut itself down. And in the case of java applications, I mean that the java application itself needs to shut down cleanly. If it does that, the JVM will automatically shut down as soon as the last thread exits.

Otherwise, as I said, no matter who kills it or how, things are likely to be left in an unpredictable mess which means probable manual clean-up and possible security exploits.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8049
    
  22

Richard Tookey wrote:Have I missed something? I see no mention by the OP that his 'gwr' process is Java related.

Which is precisely my point. Why use a Java program to do something that is basically an OS function - and furthermore, an admin one?
1. The solution will be different on different OS's.
2. The JVM may not be able to do what's required.
3. The whole notion of having processes that simply run until they're killed - unless they're daemons, in which case they should be handled with OS tools - sounds like it's out to lunch anyway.

Now, if these are Java processes, then we're in a completely different bailiwick; but that's not the impression I got.

Winston
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

The OP's third post in this thread indicates that he is using Ubuntu and if one looks a the startup scripts for many Ubuntu and other Linux distributions they just record in a file the PID of the process that they start. This is one of my suggestions to the OP but for some reason he does not acknowledge any suggestions. From my point of view this thread is dead until the OP responds with more than just asking for other possibilities without saying why the two approaches I have given him cannot be used.

Whether or not it is safe to just kill the process is up to the OP but since he gives every indication that he is happy with just killing the process I have to assume that it is safe.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

Richard Tookey wrote:... since he gives every indication that he is happy with just killing the process I have to assume that it is safe.


Happy and safe are not the same thing, unfortunately.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 265

Mahesh,

You are using ubuntu right? Did you try pkill?

See here:
GeekStuff
HowToGeek
Tecmint

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Kill Process in Unix by using name