If i have a process PID X, how can I find out what directory it was running in? I can see with ps aux | grep X
the full command line that it was invoked with. However in this case it's ./script.sh
, and I want to see which script.sh
it's running.
Bash – Linux – Find out the current working directory of a process
bashprocess
Related Topic
- Windows – How to free up a port being held open by dead process
- How to Identify Processes Generating UDP Traffic on Linux
- Linux Process Management – Keeping a Linux Process Running After Logout
- Bash – Run an interactive bash subshell with initial commands without returning to the (“super”) shell immediately
- Linux – How to search the entire system (Linux) for a particular string
- Linux – How to find out what what’s creating temporary files
Best Answer
The problem with
/proc/PID/exe
is that, for shell scripts, it shows the location of the shell. The problem with/proc/PID/cwd
is that is shows the current working directory of the process. If the process changes directories, that is reflected in the target of that symlink.This will show what the $PWD was at the time the script was started (substitute the process ID you're interested in where you see "PID"):
or simply:
This will show the command that started it so you can see if a relative or absolute directory was used:
or simply:
Together, these should show you which script is being run. For one started with
./
all you need isprocdir
.