3

How I can get PID number and folder where it works?

If I run 2 same programs in different folders:

/var/www/public_html/first_folder/test.jar <i>(it runs all the time)</i>
/var/www/public_html/second_folder/test.jar <i>(it runs all the time)</i>

If I run this command ps aux | grep test.jar

Result:

www-data  3766  0.5  3.8 2959916 75616 ?       Sl   15:01   0:13 java -jar test.jar

www-data  4239  3.4  4.1 2959916 82432 ?       Sl   15:31   0:18 java -jar test.jar

I don't know which one PID is it —- first folder or second.

3
  • Err, do you mean you want the working directory of the running process, based on the process ID (PID)?
    – ilkkachu
    Dec 20, 2017 at 13:46
  • @ilkkachu Yes.. Then I know which folder program working..
    – K Deis
    Dec 20, 2017 at 13:53
  • Buried in an answer comment is the information that the questioner does not actually want to do this at all, but actually wants to start two Java programs in some supervised fashion so that they can be easily monitored and stopped.
    – JdeBP
    Dec 20, 2017 at 22:50

3 Answers 3

6

On Linux, you can find the working directory of a process in /proc/$pid/cwd. It's a magic symlink that points to the working directory. /proc/$pid/cmdline gives the command line of the process, but it's separated with NUL bytes so needs some processing. You could do e.g. something like this to get the working directories and command lines of a number of programs matching some pgrep condition:

for pid in $(pgrep -f test.jar); do
    echo "pid: $pid cwd: $(readlink "/proc/$pid/cwd") cmd: $(tr '\0' ' ' < "/proc/$pid/cmdline")"
done

Of course if you already looked up the PID with e.g. ps, just ls -l /proc/<PID>/cwd or readlink /proc/<PID>/cwd would do.

You could also use lsof, but I don't know how to make it show the command line arguments of the program.

$ lsof -a -d cwd -c cat
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
cat     30693 someuser  cwd    DIR  253,1     4096 1700752 /tmp/y
cat     30694 someuser  cwd    DIR  253,1     4096 1700611 /tmp

On FreeBSD, this is similarly obtainable via the fstat command, although getting the actual path is somewhat trickier and slower:

for pid in $(pgrep java); do
    fstat -p "${pid}" | awk '{ if ($4 == "wd") print '"${pid}"',$5,$6; }'
done | while read -r pid root inum ; do
    printf "%s: " "${pid}"
    find -x "${root}" -type d -inum "${inum}" 2>/dev/null
done

5
  • First one gives all of java id's and I dont know which one is first_folder and second_folder.. and secon lsof doesnt work: lsof: WARNING: can't stat() tracefs file system /sys/kernel/debug/tracing Output information may be incomplete.
    – K Deis
    Dec 20, 2017 at 14:22
  • @KDeis, sorry, pgrep -f test.jar might be better. Getting the command line is slightly awkward, but we could add that to the output if it helps. Though, if the program changes working directories after starting, this of course wouldn't work.
    – ilkkachu
    Dec 20, 2017 at 14:32
  • It gives both folder test.jar PIDs and I really dont know which one is first and which second. Is it possible get pids with folder name or /var/www/public_html/first_folder
    – K Deis
    Dec 20, 2017 at 14:40
  • @KDeis, what are their working directories then? If they are the same, then we can't tell them apart from that. Another possibility would be to look at their open files, if they still hold the .jar files open after startup.
    – ilkkachu
    Dec 20, 2017 at 14:44
  • I start each program from website where I execute my java program and i need that program pid for monitoring it later so it is easier to kill it.
    – K Deis
    Dec 20, 2017 at 14:47
1
pgrep -x program_name_pattern | xargs pwdx

Explanation

  1. pgrep pattern - looks through the currently running processes and lists the process IDs which match the pattern.
    • -x, --exact - Only match processes whose names exactly match the pattern.
  2. pwdx - report current working directory of a process.

Testing

pgrep -x my_program | xargs pwdx

###Output###
15880: /home/minimax/Desktop/sandbox
15907: /home/minimax/Desktop/sandbox/yet_one_folder
0
0

lsof is your friend here:

lsof -c java | grep /var/www/public_html

The second column shows the PID.

1
  • lsof gives warning me.
    – K Deis
    Dec 20, 2017 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .