Linux pid of java

How can I find out, which jar-files java is currently running (and their PIDs)?

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 .jar s 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 ?

Note that if you run as the same user, you can attach to it using for instance visualvm and inspect it before you kill it.

4 Answers 4

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 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME less 18871 egil 4r REG 8,2 0 53862540 foo.c egil@mutter:~$ 

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

egil@mutter:~$ lsof -t foo.c 18871 

Or use it with the pids of the several java processes:

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.

You can do this native or if «lsof» is not installed via /proc//fd Example:

ps -ef|grep -w java . 0c4 6917 6916 0 12:22 pts/7 00:00:00 java . ls -la /proc/6917/fd/ total 0 dr-x------ 2 0c4 svauser 0 Apr 2 12:23 . dr-xr-xr-x 9 0c4 svauser 0 Apr 2 12:22 .. lrwx------ 1 0c4 svauser 64 Apr 2 12:23 0 -> /dev/pts/7 lrwx------ 1 0c4 svauser 64 Apr 2 12:23 1 -> /dev/pts/7 lrwx------ 1 0c4 svauser 64 Apr 2 12:23 2 -> /dev/pts/7 lr-x------ 1 0c4 svauser 64 Apr 2 12:23 3 -> /opt/jdk1.8.0_191/jre/lib/rt.jar lr-x------ 1 0c4 svauser 64 Apr 2 12:23 4 -> /media/veracrypt1/Downloads/rr/testone.jar lr-x------ 1 0c4 svauser 64 Apr 2 12:23 5 -> /usr/share/java/gnu-getopt-1.0.14.jar 

Источник

Ubuntu Java: Find a specific program’s pid and kill the program

I’m trying to make an application that checks if this specific application is running, then kill the app after a specified amount of time. I’m planning to get the pid of the app. How can I get the pid of the app? Thanks

8 Answers 8

You might try ps -aux | grep foobar for getting the pid, then issuing the kill command against it, alternatively you might want to use pkill foobar , in both cases foobar being the name of the app you want to terminate.

pid’s for java processes -e.g ps -aux | grep java | awk » .

You can also call jps which will give you a process list. A drawback of this is that it will only show you processes for the user issuing the jps command.

Читайте также:  Epson m1100 драйвер linux

you can use pidof to get the pid of your application

public static void killAll(String process) < try < Vectorcommands = new Vector(); commands.add("pidof"); commands.add(process); ProcessBuilder pb = new ProcessBuilder(commands); Process pr = pb.start(); pr.waitFor(); if (pr.exitValue() != 0) return; BufferedReader outReader = new BufferedReader(new InputStreamReader(pr.getInputStream())); for (String pid : outReader.readLine().trim().split(" ")) < log.info("Killing pid: "+pid); Runtime.getRuntime().exec("kill " + pid).waitFor(); >> catch (Exception e) < e.printStackTrace(); >> > 

If you’re starting this application from script you can get the pid of the last process that was created in background by using the special variable $! , this value you can save on a file for later use on your shutdown function.

nohup java -jar example.jar & echo $! > application.pid 

in Ubuntu, ps -aux throws an syntax error, where ps aux works.

the output is piped to awk which matches lines with java and sleeps for 10seconds and then kills the program with the pId. notice the pipe to bash. Feel free to specify however long you want, or to call what ever other calls you feel appropriate. I notice most of the other answers neglected to catch the ‘after a certain amount of time’ part of the quesiton.

you could also accomplish this by calling pidof java | awk » | bash but the choice is yours. I generally use ps aux.

You’ll probably have to invoke the ps and kill Unix commands using Runtime.exec and scrape their output. I don’t think a Java program can easily inspect / access processes it didn’t create.

you can refer to this link. it tell you about finding a pid of specific class name

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to know the process PID in linux machine

I am launching an installer from java to install a software on linux, I want to to know the «process id and image name» of it. In windows I able to get it by using tasklist command, but in linux I have not been able to do the same. I tried with ps -ef and ps -A commands, but its not showing the image name or PID of process. Is there any command to get it these values?

5 Answers 5

Most modern Linux distros have handy command pgrep (process grep), which was created just for this purpose. Use it like this:

Unlike naive ps ax | grep programname , pgrep knows how to not display process id of itself.

There is also complementary utility pkill , which can kill process by name (or send other signals).

I tried it, it didn’t worked. Here the thing is I even don’t know the process name, say for example in windows I launched a installer called xxx.exe, then I am able ti see the procee title as «xxx»(window title/process name) with that I am recognizing the windows existance. In the same way I want to do it for linux(but noprocess existed with title name) Is there any command to get it –

You should know process name or program name. If you don’t, carefully inspect output of ps ax . Also, starting top or atop in another console might shed some light in what is actual process name for the installer — it is most likely to be top process once install begins.

Читайте также:  Intermec pc23d драйвер linux

That’s what I told you — start top in another console, then start your installer, quickly switch back to top and watch which process is using most of CPU.

if it is a graphical installer you can run this one-liner in a terminal:

ps -fp $(xprop _NET_WM_PID | cut -d' ' -f3) 

your mouse cursor will change to a crosshair. Now click on the window in question. you should now see process information printed in the terminal.

Try ps -aef | grep your_process_name , replace your_process_name with your installer name.

Hi Alankar, Here the thing is I even don’t know the process name, say for example in windows I launched a installer called xxx.exe, then I am able ti see the procee title as «xxx»(window title/process name) with that I am recognizing the windows existance. In the same way I want to do it for linux(but no process existed with title name) Is there any command to get it

through command prompt like first I am navigating to the installer folder and then I am executing «./xxx.bin» command

Often (e.g. for NVIDIA drivers) the *.run is a huge sh script, so with ps auxw you see the sh process; also top will show you all the most active processes.

It is generally best to capture the process ID at the time you launch it rather than trying to find it later on.

If using the C API, you get the PID of the spawned process as the return value from fork or vfork in the mother process.

In a Bourne derived shell you use $! immediately after starting it.

I am using java to launch the installer in linux machine like this runtime.exe(«xxx.bin»). so, through java also can i get the PId\process name

Can’t help you much with Java. There are at least two older Stack Overflow threads on the subject, but neither has a really good answer.

It seems that your are looking for a Linux centric solution rather than a portable one. On Linux the easy, efficient and reliable way to get the information you need from Java is to use the proc filesystem, and especially the /proc/self directory. The man 5 proc manpage describe each file.

The pid is first field of /proc/self/stat . The image name is the second one.

It is very easy to write a class to wrap these calls. It will be only a few lines of code doing basic IO: fast and robust. Using third party programs is harder to get right and less efficient.

This solution is not portable since most of the Unix systems do not have a proc filesystem. You have four options from the best to the worst, IMHO:

  • Capture the information you need before starting the process: Could be easy or not depending of your application
  • Find clean & system specific implementations like the proc file system for Linux
  • Use unspecified API. RuntimeMXBean.getName() usually returns the pid , you can also use reflection to get access to java.lang.UnixProcess.pid . Implementation can differs between JVM and can change at any time. Acceptable solutions if you control the environment.
  • Write native code to access the POSIX API
  • Use external tools like ps
Читайте также:  Pantum driver linux rpm

If you decide to rely on external tools then you should be very careful about portability (options and output can differs greatly between two OS) and locale settings.

Источник

How can I correctly retrieve the PID of a process related to Java using ps aux? and how can I put the headr containing the output column name?

I am not a system administrator (I am a software developer) and I have the following doubt. Performing this command I can see the list of the first 10 process running on my system (correct me if I am doing wrong assertion):

MYSUER@MYHOST.it [~/FOLDER]# ps aux | head -10 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 191292 3892 ? Ss 2021 21:29 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 root 2 0.0 0.0 0 0 ? S 2021 0:00 [kthreadd] root 4 0.0 0.0 0 0 ? S< 2021 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? S 2021 2:17 [ksoftirqd/0] root 7 0.0 0.0 0 0 ? S 2021 0:10 [migration/0] root 8 0.0 0.0 0 0 ? S 2021 0:00 [rcu_bh] root 9 0.1 0.0 0 0 ? S 2021 71:37 [rcu_sched] root 10 0.0 0.0 0 0 ? S< 2021 0:00 [lru-add-drain] root 11 0.0 0.0 0 0 ? S 2021 0:56 [watchdog/0] 

So the second column is the PID identifieng the process (I can use it to kill a process). Then I tried to perform this command in order to retrieve all the running process related to JAVA:

MYSUER@MYHOST.spaccount.it [~/FOLDER]# ps aux | grep java webadmin 22316 0.0 0.0 112812 980 pts/0 S+ 18:21 0:00 grep --color=auto java 

Basically I am filtering only the line related to java And here a doubt. Tyhe PID is always the second column value? Is it possible to put the header containing the column name as done in the first snippet?

2 Answers 2

The short answer is you do not use ps aux for that.

Usually such processes would be run as services, and their PID tracked by the service manager (e.g. systemd; Upstart; supervisord; Monit. ) If the goal is to check whether the service is running – the service manager can answer that. (For example, systemctl is-active MyApp .) If the goal is to restart the service if it crashes, or to prevent the service from being run twice – it's the service manager's job to do that.

In situations where the process cannot be run as service, its PID should be immediately recorded on startup to a PID file – either the app itself, or the launcher, can do this. (For example, java -jar myapp.jar & echo $! > java.pid .)

When you really need to search all running processes, use the pgrep command. For example, to find all Java processes use pgrep java and to find all processes that mention MyApp.jar, use pgrep -f MyApp.jar . (Add the -a option to see the command, e.g. pgrep -a java .) This won't have the problem of returning grep itself, the way ps|grep does.

If you absolutely must use ps , then specify the exact columns that you want. Now the u option already requests a specific format, with certain columns in certain order – but if all you want is the PID, the o option lets you get just the PID. For example, ps axo pid,cmd always results in exactly two columns: the PID and the command, in that order.

Источник

Оцените статью
Adblock
detector