Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Mar 29 '13 at 12:40

This question came from our site for professional and enthusiast programmers.

4 Answers 4

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

pgrep -lf programname

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).

share|improve this answer
    
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 – –  kgopi Mar 29 '13 at 5:45
    
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. –  mvp Mar 29 '13 at 5:49
    
I want to the know process name of my installer program –  kgopi Mar 29 '13 at 6:04
    
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. –  mvp Mar 29 '13 at 6:06

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

Hope this helps !!

share|improve this answer
    
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 –  kgopi Mar 29 '13 at 5:43
    
@kgopi. How you are starting your installer in linux. –  Alankar Mar 29 '13 at 5:50
    
through command prompt like first I am navigating to the installer folder and then I am executing "./xxx.bin" command –  kgopi Mar 29 '13 at 5:53
    
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... –  Basile Starynkevitch Mar 29 '13 at 5:56
1  
@kgopi Then you can give that xxx only after grep something like this pa -aef | grep -i xxx –  Alankar Mar 29 '13 at 5:57

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.

share|improve this answer
    
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 –  kgopi Mar 29 '13 at 6:07
    
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. –  dmckee Mar 29 '13 at 6:15

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

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.

share|improve this answer

Your Answer

 
discard

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.