I have a number of ruby processes running:

ps -A | grep ruby 
 3518 ?        00:00:12 ruby
10316 ?        00:00:00 ruby
22400 pts/5    00:00:45 ruby
23332 ?        00:00:07 ruby

I get the pid above, but I want to know WHERE these processes are in the filesystem. In other words, where they were executed.

Why do I want to know? I have a daemon running in a byobu screen and I want to know where it was executed from.

byobu new -s daemon
ls -l
  -rwxrwxr-x 1 someuser someuser   83 Jul  2 11:13 db_service.sh
cat db_service.sh
  #!/bin/sh
  RAILS_ENV=production bundle exec ruby lib/daemons/db_service_ctl start
./script/db_service.sh

So as you can see from the above bash commands, a daemon was spawned (ruby on rails daemon) from the db_service.sh shell script. So where in the filesystem was it spawned from? Can ps help me here or is there a better program in linux to find out the desired information? I am not looking for the path of the ruby installation, but rather the path at which a ruby instance was executed

share|improve this question
    
Are you looking for who executed the process (scripts for example) or where the executable of the process is found when it was executed? Or something else? For example, does "ps -ex" help you? – Luis Alvarado Jul 9 '13 at 23:06
    
You could always just add pid.lock or pid.name files in temp for your ruby instance instead of trying to go at it the other way. – RobotHumans Jul 9 '13 at 23:26
    
" where the executable of the process is found when it was executed" thats what I want to know @LuisAlvarado – JohnMerlino Jul 10 '13 at 0:17
    
@JohnMerlino - can you check Mik's answer/script for usefulness? If it does what you wanted, I'll award the bounty I discussed in comments and gift him some rep for helping future visitors. Thanks. – RobotHumans Jul 12 '13 at 3:18
    
Is the script useful or have you lost interest in the question, as usually happens in these cases? – user76204 Jul 26 '13 at 22:52
up vote 3 down vote accepted

So you got the process id, you can then look around the /proc/ virtual file system. Everything is there. For e.g.

  • /proc/23124/cwd - the current directory of process 23124
  • /proc/23124/cmdline - the full command line of the process.
share|improve this answer
    
Nice. I'ld bounty it for 50 if you added a script that outputted the pid, cwd, and a the cmdline tab delimited. – RobotHumans Jul 9 '13 at 23:21
    
@CallmeV Do you want the output of cwd or only the location as in /proc/123/cwd. I ask since, for some processes, if you enter cwd it will show you many different things. for example, entering the main chrome process will end up in your home folder and it will show you all file/folders in there which is not nice if you want to do something with that tab limited information. – Luis Alvarado Jul 10 '13 at 0:31
    
Something that produces logical output fairly consistently for running applications on my box would get the 50, if I had no outside input. E.g. - it should be parser friendly/useful to the OP. I'll depend on him saying it's useful to him on the bounty award, as I really have no dog in this fight. I wouldn't use bash, I'ld use PSI. – RobotHumans Jul 10 '13 at 0:39
    
the cmdline output was useful: /proc/26501 $ cat cmdline rubylib/daemons/db_service_ctlstart% – JohnMerlino Jul 10 '13 at 16:47
    
So there's no built in command that will output pid, cwd, and cmdline by default I take it? – JohnMerlino Jul 10 '13 at 16:48

Even though /proc has been mentioned briefly above as a source of information for learning about processes on a system, I include here a basic Bash script to parse some information out of it. For an excellent introduction to the /proc virtual filesystem, see this IBM article, which explains how it can be useful for both users and programmers.

Parsing /proc with a generic script can be unreliable, as different processes often have different /proc layouts, but basic information can usually be gleaned with this script, although it may have to be modified if you want to parse any other specific information. I have written it using Bash, but there are better parsers which you could investigate if you want to write it using a different scripting language.

Often in the output of the script you may see a "permission denied" message, which means the /proc location is not readable by $USER, so you can run the script as root if you want in that case to gain the full output.

Copy the body of the script into a new file, save it, and make it executable (chmod u+x) and then call it with at least one argument (it now can handle multiple processes at the same time):

./proc_script xfce4-panel

or put it in your $HOME/bin and then you can run it like other programs if $HOME/bin is in your path. (However, if you use sudo you will still have to give the absolute path location of the script, as $HOME/bin is not in root's path.)

The body of the script; it is also available and updated at my Github page:

#!/usr/bin/env bash  

(( $# == 0 )) && { printf "Usage: Please specify the name of one process \
to research.\n" >&2; exit 1; }

args=("$@")                   
for process in "${args[@]}"; do                    
    # store user input in a variable and warn then exit if program is not running
    queried_pid="$(pgrep "${process}" || { printf '%s\n' "No such program" >&2; })"

    # add the contents of the variable into an array
    pidarray=($(echo "$queried_pid"))

    # iterate over the array, however many pids have been found
    for i in "${pidarray[@]}"; do 
        printf "Pid is: %s\nExe is: %s\nCmdline is: %s\ncwd is: %s\n" "$(echo "$i")" \
    "$(stat -c %N /proc/"$i"/exe)" "$(cat /proc/"$i"/cmdline)" "$(cd /proc/"$i"/cwd/; pwd -P)"
    done
    printf "\n\n"
done

exit 

Sample output 1:

./proc_parse firefox


Pid is: 1830
Exe is: `/proc/1830/exe' -> `/usr/lib/firefox/firefox'
Cmdline is: /usr/lib/firefox/firefox
cwd is: /home/mike

Sample Output 2 (dealing with multiple pids):

./proc_parse evince

Pid is: 3113
Exe is: `/proc/3113/exe' -> `/usr/bin/evince'
Cmdline is: evince/home/mike/Z_IBM_lpic_Linux_pdfs/l-proc-pdf.pdf
cwd is: /home/mike
Pid is: 3119
Exe is: `/proc/3119/exe' -> `/usr/lib/evince/evinced'
Cmdline is: /usr/lib/evince/evinced
cwd is: /
share|improve this answer
    
Thanks. I said something in chat about the OP needing to find it useful, since I'ld just use python with PSI to accomplish the same...but if the OP finds it useful, you'll get the bounty I discussed. Upvote – RobotHumans Jul 12 '13 at 3:16

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.