others-How to show full path or current working directory of a process in linux
1. Purpose
In this post, I would demo how to show the full running path or current working directory of a process in linux. Before start, we should know the definition of the phrase working directory
:
In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with each process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd(3) function, or just current directory.
2. The problem and solutions
2.1 The problem
When we use ps -ef
or top
command to show processes in the linux system, we can only see the command name instead of the full path of the command, just as follows:
[[email protected] ~]# ps -ef|grep java
root 24572 1 0 Oct11 ? 01:47:52 java -Xmx500m -Xms256m -cp ./libs/*:. com.bswen.WebApplication
We can only see the java
process is running, but where does it run? e.g. We want to know which directory the java process is running in.
2.2 The solution #1
We can use the following command to print the working directory of the process:
pwdx <PID>
For example, our process’s PID is 24572
, so we can check the working directory of the process as follows:
[[email protected] ~]# pwdx 24572
24572: /opt/tweb
How does it work?
The pwdx command reports the current working directory of a process or processes.
When Linux starts a process, the system will create a folder named PID under the /proc
directory, just as follows:
[[email protected] ~]# ls /proc/24572
attr cmdline environ io mem ns pagemap schedstat stat timers
autogroup comm exe limits mountinfo numa_maps personality sessionid statm timerslack_ns
auxv coredump_filter fd loginuid mounts oom_adj projid_map setgroups status uid_map
cgroup cpuset fdinfo map_files mountstats oom_score root smaps syscall wchan
clear_refs cwd gid_map maps net oom_score_adj sched stack task
[[email protected] ~]#
Here are some important file/subdirectories in the /proc/<PID>
directory:
-
The
cwd
symbolic link is the process running directory; - The
exe
symbolic link is the absolute path of the execution program; - The
cmdline
is the command line command entered when the program is running; - The
environ
records the environment variables when the process is running; - The
fd
directory is a symbolic link of files opened or used by the process.
2.3 The solution #2
Just as the above explanations, we can just use ls -l
to print the absolute path of the process:
ll /proc/<PID>/exe
2.4 The solution #3
And we can also use the lsof -p
command to print the current working directory of the process:
lsof -p <PID> | grep cwd
For example:
[[email protected] ~]# lsof -p 24572|grep cwd
java 24572 root cwd DIR 8,2 4096 274362 /opt/tweb
[[email protected] ~]#
What is lsof
?
lsof
command stands for List Of Open File. This command provides a list of files that are opened. Basically, it gives the information to find out the files which are opened by which process
What does lsof -p
do?
List all open files that are opened by a particular process: Each file is associated with some process ID. There can be many files that are opened by a particular process. By using
lsof -p
process ID, files opened by a particular process can be checked.
We can see the result of lsof -p 24572
as follows:
[[email protected] ~]# lsof -p 24572
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 24572 root cwd DIR 8,2 4096 274362 /opt/tweb
3. Summary
In this post, I demonstrated how to print the current working directory of a process in linux system, the key point is understand where the linux system store the working directory information, then we can use pwdx
or lsof
commands to show it. That’s it, thanks for your reading.