2

Im running a jar file as part of a large web app. The majority of the app is written in php, but there is one large .jar file that it interacts with. To start this jar file I use ssh to connect to the server, navigate to the directory and run it by calling:

java -jar file_name.jar

If I want to turn off this file, what's the ssh command for that ?

Kitalda
  • 351
  • 2
  • 18
sam
  • 9,486
  • 36
  • 109
  • 160
  • 1
    Basically `kill -9 PID` (and milder variants) Where PID is the process id of the java process... ([relevant post](http://stackoverflow.com/questions/2541597/how-to-gracefully-handle-the-sigkill-signal-in-java)) Or you could probably do some JMX based graceful shutdown too. – ppeterka Sep 12 '13 at 11:14

2 Answers2

3

While agreeing with other comments and answers, I'd like to point out the oft forgotten jps tool packaged with JDK's

anders@localhost:~$ jps -v
15688 Jps -Dapplication.home=/usr/lib/jvm/java-7-oracle -Xms8m

which lists all running Java processes on the host (might want to sudo if the process wasn't started by your login user).

So, with some command line magic such as

kill -9 `jps -v | grep file_name.jar | awk {'print $1'}`

you would achieve your stated purpose.

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
2

If you do:

ps aux

or something similar (see man ps for the many different possible commands) you should be able to find the PID of the java process (might be difficult if there are many java processes running*). Then do:

kill PID

If that doesn't work, try:

kill -9 PID

But this will not give the process a chance to shut down cleanly.

*) The reason this might be difficult with many java processes running, is that on some OS's, Java versions, etc, the process name might simply be "java", which makes it hard to distinguish them.

Update: Or you can use pgrep -lf file_name.jar to get the PID easier.
See https://linux.die.net/man/1/pgrep

Svend Hansen
  • 3,277
  • 3
  • 31
  • 50
  • 1
    With `pgrep -lf file_name.jar` you'll find the PID faster. – Frank Kusters Sep 12 '13 at 11:19
  • I wasn't aware of `pgrep`. Sounds like a useful command. I used to do `ps aux | grep [KEY]` when I needed, but this would again be a problem if the java processes haven't got helpful names, but I'm not sure if that's a problem much (I just encountered it once or twice in the past) :) – Svend Hansen Sep 12 '13 at 11:26