Shell – What’s the best way to determine the working directory for UNIX process using ‘ps’

command lineprocesspsrubyshell-script

I am running a Ruby command line script (rufus.sh)which ultimately calls Thread.new, which spawns a UNIX process as shown below. I run this script for more than 1 directory as the output of the ps command shows below. The last (right most) column below is COMMAND (COMD) which is essentially the name of the process. The problem is that COMMAND doesn't include the working directory (the current directory when the process was launched). What is the best way to figure that out or include it in COMMAND? Thanks, Chirag

ps -ef | grep runner
web       7532     1  0 Oct08 ?        00:02:08 /usr/local/bin/ruby ./script/runner ./config/jobs/control.rb critical_alert start
web      21114     1  1 Oct09 ?        02:49:28 /usr/local/bin/ruby ./script/runner ./config/jobs/control.rb task start
web      19028     1  1 10:45 pts/0    00:00:34 /usr/local/bin/ruby ./script/runner ./config/jobs/control.rb task start
web      19029     1  0 10:45 pts/0    00:00:10 /usr/local/bin/ruby ./script/runner ./config/jobs/control.rb critical_alert start

Best Answer

I'm not sure if that answers your question, but the pwdx could give you the current working directory of any process.

Like this:

pwdx <PID>

You could also do what Ulrich Schwarz said and run

ls -l /proc/PID/cwd
Related Question