Find the answer to your Linux question:
Results 1 to 3 of 3
I am currently running Ubuntu 12.04 and am trying to create a script to output the names of running processes along with their working directories into a file the as ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3

    Lightbulb how to output a process's working directory to a file?


    I am currently running Ubuntu 12.04 and am trying to create a script to output the names of running processes along with their working directories into a file the as such:

    Code:
    Process            Working Directory
    firefox          /home/pepper/firefox
    The reason behind this script is to allow me to have access to the working directories of running processes so that I can tell where they are coming from. I'm relatively new to terminal scripting so i'd like to know if i'm on the right track.
    (I don't know the working directory of firefox, so i used the path above as an example of the format i need.)

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,387
    Hi and welcome.

    You write "working directory", which means the current directory of a process.
    It can be changed by the process during runtime, as you can see:
    Code:
    #!/usr/bin/env bash
    
    pwd
    cd /tmp
    pwd
    The command ps can list the environment for processes, including pwd:
    Code:
    ps -auwwxe
    But there is also a more specialized command: pwdx
    Code:
    pwdx <PID_OF_PROCESS>
    If all else fails, you can look up the softlink in /proc:
    ls -ld /proc/<PID_OF_PROCESS>/cwd


    Can you please clarify the purpose of your script?
    "Where they are coming from" seems a bit vague, as the cwd can change.
    Do you mean:
    1) What is the cwd at the *start* of a programm/script? This can be done by printing the cwd as the user in question and before the script/programm execution.
    Or if all else fails by using a small wrapper that prints the cwd before executing the script/programm.
    2) the $PATH of a script/programm. This can be determined by using "which", "find" or the package manager
    e.g.
    Code:
    which firefox
    /usr/bin/firefox
    
    dpkg -L firefox | grep bin
    /etc/apparmor.d/usr.bin.firefox
    /usr/bin
    /usr/lib/firefox/distribution/searchplugins/locale/en-US/bing.xml
    /usr/lib/firefox/components/binary.manifest
    /usr/bin/firefox
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3
    Basically, I'm trying to write a script that will help me understand the way programs work by providing me with information about where they are located on the drive. Maybe I'm missing something about how linux operates, I'm not sure. I'm rather new with some parts of linux.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •