others-How to show full path or current working directory of a process in linux
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
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:
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
cwdsymbolic link is the process running directory;
exesymbolic link is the absolute path of the execution program;
cmdlineis the command line command entered when the program is running;
environrecords the environment variables when the process is running;
fddirectory 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:
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
[[email protected] ~]# lsof -p 24572|grep cwd java 24572 root cwd DIR 8,2 4096 274362 /opt/tweb [[email protected] ~]#
lsofcommand 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
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 -pprocess 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
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
lsof commands to show it. That’s it, thanks for your reading.