Operating System - HP-UX
1748159 Members
3943 Online
108758 Solutions
New Discussion юеВ

kill a process by its name

 
Miguel Silva Rentes
Regular Advisor

kill a process by its name

Hi everyone!

I'm launching a java process through a script (to set classpath, environment vars, etc). This script receives as an argument the name of the class I want to execute.
I have severall processes being launched using this mechanism.


I'm trying to kill a child process (java) but when I use "ps -ex | grep java" the command line which gives me the classpath of all .jars used by this java child process is too short. I know the specific class that was used to launch that java process. However, when I do "ps -ex | grep java" I get similar lines for all running java processes because there are severall similar JARs in the classpath and the command line displayed is truncated in the ps. So I cannot clearly identify the PID of the process I want to kill.


Is there any way to kill a particular java process in this scenario? Or kill the child of the script used to launch it (the script is still in the ps as long as the process is running).

Best regards,

Miguel Rentes
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: kill a process by its name

Would the "-x" option, to show the command line in extended format, help?


Pete

Pete
Dennis Handly
Acclaimed Contributor

Re: kill a process by its name

>I'm trying to kill a child process (java) but when I use "ps -ex | grep java" the

You don't want to use grep. Use -C:
$ UNIX95=EXTENDED_PS ps -xC java

>I get similar lines for all running java processes ... Or kill the child of the script used to launch it

Then you'll have to find it by the parent:
$ UNIX95=EXTENDED_PS ps -xC script

If you want to find all children:
$ UNIX95=EXTENDED_PS ps -ef -ocomm= -oppid= | fgrep $(UNIX95= ps -x -C script -opid=)

Miguel Silva Rentes
Regular Advisor

Re: kill a process by its name

Hi,

I can't use ps -xC java. I get the following error:

ps: illegal option -- C
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]

Reading the "man ps" I can see I can only have -C or -o in XPG4 (which I assume I haven't available).

Any other ideas?

Best regards,

Miguel Rentes
Patrick Wallek
Honored Contributor

Re: kill a process by its name

Look at Dennis' response again.

He precedes the 'ps' command with 'UNIX95= '.

The 'UNIX95= ' (NOTE the space AFTER the = sign') turns on the XPG4 behavior for the command.
Miguel Silva Rentes
Regular Advisor

Re: kill a process by its name

Humm... I can't use UNIX95 either:

$ UNIX95= ps -xC java
UNIX95=: Command not found.

I'm using tcsh and the TERM variable is set to xterm (can it have any influence?)...
OldSchool
Honored Contributor

Re: kill a process by its name

tcsh handles environment vars differently, i believe. try:


setenv UNIX95 1
ps -xC java
unsetenv UNIX95


hopefully you haven't changed "root"s default shell to tcsh. if you have, change it back to /sbin/sh immediately! Disaster lies down that path
Dennis Handly
Acclaimed Contributor

Re: kill a process by its name

>I'm using tcsh

If you aren't use a real shell but the scummy csh or its derivatives, you need to mention that. (That's why I use a "$" prompt in my examples.)
Miguel Silva Rentes
Regular Advisor

Re: kill a process by its name

Hi everyone,

Doing a

$ setenv UNIX95 1

I could in fact use "ps -xC java" and all the command lines you told me to test. Thank you very much!

By the way, my root user is using the /sbin/sh and all other users have /usr/local/bin/tcsh.

Best regards,

Miguel Rentes
Dennis Handly
Acclaimed Contributor

Re: kill a process by its name

If you are happy with the answers given please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33