Linux process work directory

How can I know the absolute path of a running process?

If I have multiple copies of the same application on the disk, and only one is running, as I can see with ps , how can I know the absolute path to distinguish it from the others?

8 Answers 8

% ps -auxwe | grep 24466 root 24466 0.0 0.0 1476 280 ? S 2009 0:00 supervise sshd % sudo ls -l /proc/24466/exe lrwxrwxrwx 1 root root 0 Feb 1 18:05 /proc/24466/exe -> /package/admin/daemontools-0.76/command/supervise

@jarno ls: cannot read symbolic link /proc/28783/exe: Permission denied — it’s not about running the ls command, it’s about accessing the process info of a process not belonging to you. On my box, about 97% of all processes listed in /proc are root processes, and the others are distributed over 11 different users.

ls: cannot read symbolic link ‘/proc/87/exe’: No such file or directory lrwxrwxrwx 1 root root 0 Oct 9 07:05 /proc/87/exe

This gives you the current working directory of the pid, not its absolute path.

Usually the which command will tell you which is being invoked from the shell:

@Kokizzu No, it doesn’t because it doesn’t answer the question at all. The which command only tells you which binary will be run if you execute the command now. The question was «which binary is already running there». Imagine for example having a dozen jdks on your computer. If you want to know for a running java process which jdk it’s been taken from, which doesn’t help you with that. It will only tell you which jdk it will be taken from, if you execute it now. The accepted answer is also the correct one.

An obvious way this answer is wrong: on my machine I run processes with different JDK versions and some 32bits/64bits. If I want to identify the correct jstack/jmap version for the process the answer above will not work while the accepted answer will.

@Kokizzu This only answers the question, «What is the current working directory of the process $pid ?» The edited post still doesn’t answer the question. which merely tells «If the command is on the path, then what is it?»

pwdx return me the absolute path of the exectuable program of the process depending on pid on redhat x64 6.3.

Читайте также:  Готовый linux интернет шлюз

Источник

How to find a working directory of running process(HPUX/Solaris/Linux/AIX)

Trying to get the home directory of running process. For Linux, I learned that I could use /proc/PID/exe information, but I think there is no that information in other OS. Assuming that there is no file information $PATH, can you let me know how I can get the home directory of the running process? I just need to assume that OS utilities usage is very limited in OS, meaning that I should use very common command. Condition: No special utility such as lsof.

Added

1 Answer 1

The first column for ps -ef (the most common useful-options, in POSIX) give you the process owner, usually a name (sometimes only the uid number). For example

UID PID PPID C STIME TTY TIME CMD statd 1935 1 0 04:00 ? 00:00:00 /sbin/rpc.statd 101 2329 1 0 04:00 ? 00:00:00 /usr/bin/dbus-daemon --system daemon 2511 1 0 04:00 ? 00:00:00 /usr/sbin/atd avahi 2540 1 0 04:01 ? 00:00:00 avahi-daemon: running [vmw-de> avahi 2541 2540 0 04:01 ? 00:00:00 avahi-daemon: chroot helper bind 2593 1 0 04:01 ? 00:00:00 /usr/sbin/named -u bind kdm 2781 2780 0 04:01 ? 00:00:01 /usr/lib/kde4/libexec/kdm_gre> www-data 2903 2782 0 04:01 ? 00:00:00 /usr/sbin/apache2 -k start www-data 2904 2782 0 04:01 ? 00:00:00 /usr/sbin/apache2 -k start www-data 2905 2782 0 04:01 ? 00:00:00 /usr/sbin/apache2 -k start www-data 2906 2782 0 04:01 ? 00:00:00 /usr/sbin/apache2 -k start www-data 2908 2782 0 04:01 ? 00:00:00 /usr/sbin/apache2 -k start ntp 2989 1 0 04:01 ? 00:00:00 /usr/sbin/ntpd -p /var/run/nt> postgres 3059 1 0 04:01 ? 00:00:00 /usr/lib/postgresql/9.1/bin/p> postgres 3063 3059 0 04:01 ? 00:00:00 postgres: writer process > postgres 3064 3059 0 04:01 ? 00:00:00 postgres: wal writer process > postgres 3065 3059 0 04:01 ? 00:00:00 postgres: autovacuum launcher> postgres 3066 3059 0 04:01 ? 00:00:00 postgres: stats collector pro> 104 3555 1 0 04:01 ? 00:00:00 /usr/sbin/exim4 -bd -q30m gitlog 3677 3676 0 04:01 ? 00:00:00 svlogd -tt /var/log/git-daemon 116 3679 3676 0 04:01 ? 00:00:00 /usr/lib/git-core/git-daemon 

The process owner name (or uid number) are in /etc/passwd as the first column (for the name) or the third column (uid number). Columns in /etc/passwd are delimited by colons ( : ). For example:

root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh www-data:x:33:33:www-data:/var/www:/bin/sh backup:x:34:34:backup:/var/backups:/bin/sh list:x:38:38:Mailing List Manager:/var/list:/bin/sh irc:x:39:39:ircd:/var/run/ircd:/bin/sh gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh nobody:x:65534:65534:nobody:/nonexistent:/bin/sh libuuid:x:100:101::/var/lib/libuuid:/bin/sh messagebus:x:101:105::/var/run/dbus:/bin/false colord:x:102:106:colord colour management daemon. /var/lib/colord:/bin/false usbmux:x:103:46:usbmux daemon. /home/usbmux:/bin/false Debian-exim:x:104:111::/var/spool/exim4:/bin/false statd:x:105:65534::/var/lib/nfs:/bin/false avahi:x:106:114:Avahi mDNS daemon. /var/run/avahi-daemon:/bin/false 
statd:x:105:65534::/var/lib/nfs:/bin/false 

The next-to-last column of /etc/passwd is the home directory of the process, e.g., /var/lib/nfs for the statd process.

Some system processes have no home directory, e.g., you may see /usr/sbin on Linux systems, or some other directory that several processes share.

  • POSIX ps shows the options of POSIX ps , implemented in these systems:
  • HPUX ps
  • Solaris ps
  • Linux ps
  • AIX ps
  • passwd(5) shows the file-format of /etc/passwd
Читайте также:  Найти путь до файла linux

OP amended question to indicate that the current directory (rather than home-directory) is wanted. Systems using a proc-filesystem can provide this information. Those are Solaris, AIX and Linux.

However, HPUX does not (see for example /proc on HP-UX?, which says the pstat system-call can be used). I do not see a possibility, reading its manual page, but the link below says pstat_getpathname will work.

AIX supports it, according to IBM documentation.

Look for the cwd «file» for the working directory of a given process in systems with a proc-filesystem.

Источник

How to get pid number and folder where it works

Err, do you mean you want the working directory of the running process, based on the process ID (PID)?

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.

3 Answers 3

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//cwd or readlink /proc//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 "$" | awk '< if ($4 == "wd") print '"$"',$5,$6; >' done | while read -r pid root inum ; do printf "%s: " "$" find -x "$" -type d -inum "$" 2>/dev/null done

Источник

Читайте также:  Crontab linux раз в день

Find out current working directory of a running process?

What command(s) can one use to find out the current working directory (CWD) of a running process? These would be commands you could use externally from the process.

7 Answers 7

There are 3 methods that I'm aware of:

pwdx

lsof

/proc

Examples

$ pwdx 12136 12136: /home/saml 
$ lsof -p 12136 | grep cwd nautilus 12136 saml cwd DIR 253,2 32768 10354689 /home/saml 

Or you can poke directly into the /proc :

$ readlink -e /proc/12136/cwd/ /home/saml 

In Ubuntu Server 18.04 the above commands require root privileges. At least in OpenBSD, you can see this answer to my U&L question.

I assume that you have the process ID in pid . Most methods on most systems will require that the shell you're doing this from is running as the same user as the target process (or root).

On Linux and Solaris and perhaps some other System V unices:

On Linux (except embedded systems where readlink is not available) but not Solaris:

On just about any unix variant, you can use lsof . Beware that if there is a newline, it will be printed as \n (indistinguishable from backslash followed by n ). If you feel lucky, you can use the second form, which silently chokes on all whitespace in the directory name.

lsof -a -Fn -p $pid -d cwd | sed -e '1d' -e '2s/^n/' lsof -p $pid | awk '$4=="cwd" ' 

Bonus: if you need to cause a process to change its current directory, you can do it with a debugger. This is useful for example to move a long-running program that doesn't care about its current directory out of a directory that you want to remove. Not all programs appreciate having their current directory changed under their feet — for example a shell is likely to crash.

#!/bin/sh # Use gdb to change the working directory of a process from outside. # This could be generalized to a lot of other things. if [ $# -ne 2 ]; then echo 1>&2 "Usage: $0 PID DIR" exit 120 fi case "$1" in *[!0-9]*) echo 1>&2 "Invalid pid \`$1'"; exit 3;; esac case "$2" in *[\\\"]*) echo 1>&2 "Unsupported character in directory name, sorry." exit 3;; esac gdb -n -pid "$1" -batch -x /dev/stdin  

Источник

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