Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a .jar file which is notorious for malfunctions. When a malfunction occurs, only a restart helps. I have a way to detect that malfunctions (reading the log-file of said .jar) So I want to write a script, that kills the process whenever the malfunction occurs. The problem is:

confus@confusion:~$ ps -A
4438 ?        00:00:00 java
4439 ?        00:00:00 java
4443 ?        00:00:00 java

The process name of all running .jars is naturally "java". How do I find out, which of these "java"-processes is the one I want to kill, i.e. the one running foobar.jar?

share|improve this question
up vote 7 down vote accepted

You can run the lsof command, which lists which processes has open files, with your jar file given as an argument. An example viewing a file with less:

egil@mutter:~$ lsof foo.c
less    18871 egil    4r   REG    8,2        0 53862540 foo.c

To easily reuse the pid in a script, you could run it in terse mode:

egil@mutter:~$ lsof -t foo.c
share|improve this answer
This answer is even better. +1. – Octavian Damiean May 20 '11 at 9:10

Using ps ax will help.

It will display the process tree in a BSD style which simply shows way more information.

To find your particular process you just have to grep for the JAR name. ps ax | grep JARNAME will do it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.