Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

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
share|improve this answer
    
Note that @Chirag asked for source directory, not for current working directory. –  andcoz Oct 18 '11 at 20:31
add comment

Maybe you will find it as the target of /proc/(pid)/cwd. It looks like it, but I'm not very at home in /proc.

share|improve this answer
    
Note that @Chirag asked for source directory, not for current working directory. –  andcoz Oct 18 '11 at 20:31
add comment

If it isn't a necessity to have all the other info, you could try this:

ps -ef | grep runner | awk {'print$8'} | xargs which

It will grab the eighth column of ps -ef (this is the COMD column on my OS X machine; you may need to change this) and run it through which.

This works on my machine as demonstrated here:

$ ps -ef | grep grep
mfc      25780 21222  0 11:54 pts/5    00:00:00 grep grep

$ ps -ef | grep grep | awk {'print$8'} 
grep

$ ps -ef | grep grep | awk {'print$8'} | xargs which
/bin/grep
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.