Reputation: 967
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.
The process I am referring is the process a 3rd-party application runs.
Thanks in advance.
Upvotes: 0
Views: 2561
Reputation: 54583
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
In this example, statd
is
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.
Further reading:
ps
, implemented in these systems:/etc/passwd
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.
Further reading:
Upvotes: 1