Skip to main content
Add `fuser` example
Source Link
Stephen Harris
  • 49.3k
  • 7
  • 115
  • 138

EDIT TO ADD:

There is a special case of this problem, where the question might be "what processes are using a mount point". This isn't reporting the cwd but any files that may be open.

So, for example:

$ sudo fuser -u -c /brick
/brick:               3552(root)
$ ps -p 3552
  PID TTY          TIME CMD
 3552 ?        00:04:51 glusterfsd
$

We know the glusterfsd process is the only one using the /brick filesystem

EDIT TO ADD:

There is a special case of this problem, where the question might be "what processes are using a mount point". This isn't reporting the cwd but any files that may be open.

So, for example:

$ sudo fuser -u -c /brick
/brick:               3552(root)
$ ps -p 3552
  PID TTY          TIME CMD
 3552 ?        00:04:51 glusterfsd
$

We know the glusterfsd process is the only one using the /brick filesystem

Source Link
Stephen Harris
  • 49.3k
  • 7
  • 115
  • 138

The jobs command may show working directory the program started in, if that directory is different to the shell's current one. That's because the shell is tracking what it knows (where the process started), not the current state.

It's also dependent on the shell.

eg with bash

bash-4.4$ cd /tmp
bash-4.4$ sleep 1000 &
[1] 24807
bash-4.4$ jobs -l
[1]+ 24807 Running                 sleep 1000 &
bash-4.4$ cd /
bash-4.4$ jobs -l
[1]+ 24807 Running                 sleep 1000 &  (wd: /tmp)

But ksh93 doesn't do that

$ sleep 1000 &
[1]     7164
$ jobs -l
[1] + 7164       Running                 sleep 1000 &
$ cd /
$ jobs -l
[1] + 7164       Running                 sleep 1000 &
$ 

I don't believe there's any portable way of finding the cwd of a process. fstat, pwdx and similar may help. You may need root privileges to look at processes you don't own.

On Linux, the processes matching a specific path may be used by looking at the /proc/.../cwd symlink:

eg to find processes with /tmp in the path:

% sudo ls -l /proc/*/cwd | grep /tmp
lrwxrwxrwx. 1 sweh     sweh     0 Jul 28 09:38 /proc/23435/cwd -> /news/tmp
lrwxrwxrwx. 1 sweh     sweh     0 Jul 28 09:39 /proc/7124/cwd -> /news/tmp

Remember this may not match the process's internal representation of the directory because of symlinks:

$ cd /usr/tmp
$ pwd
/usr/tmp
$ ls -l /proc/self/cwd
lrwxrwxrwx. 1 sweh sweh 0 Jul 28 09:41 /proc/self/cwd -> /var/tmp/
$ ls -l /usr/tmp
lrwxrwxrwx 1 root root 10 May 13 09:39 /usr/tmp -> ../var/tmp/
$ 

Here the shell thinks I'm in /usr/tmp but really it's /var/tmp.