How do I get the path of a process in Unix / Linux

后端 未结 11 590
无人共我
无人共我 2020-12-02 04:31

In Windows environment there is an API to obtain the path which is running a process. Is there something similar in Unix / Linux?

Or is there some other way to do th

相关标签:
11条回答
  • 2020-12-02 04:56

    The below command search for the name of the process in the running process list,and redirect the pid to pwdx command to find the location of the process.

    ps -ef | grep "abc" |grep -v grep| awk '{print $2}' | xargs pwdx
    

    Replace "abc" with your specific pattern.

    Alternatively, if you could configure it as a function in .bashrc, you may find in handy to use if you need this to be used frequently.

    ps1() { ps -ef | grep "$1" |grep -v grep| awk '{print $2}' | xargs pwdx; }
    

    For eg:

    [admin@myserver:/home2/Avro/AvroGen]$ ps1 nifi
    
    18404: /home2/Avro/NIFI
    

    Hope this helps someone sometime.....

    0 讨论(0)
  • 2020-12-02 04:59

    On Linux, the symlink /proc/<pid>/exe has the path of the executable. Use the command readlink -f /proc/<pid>/exe to get the value.

    On AIX, this file does not exist. You could compare cksum <actual path to binary> and cksum /proc/<pid>/object/a.out.

    0 讨论(0)
  • 2020-12-02 05:04

    A little bit late, but all the answers were specific to linux.

    If you need also unix, then you need this:

    char * getExecPath (char * path,size_t dest_len, char * argv0)
    {
        char * baseName = NULL;
        char * systemPath = NULL;
        char * candidateDir = NULL;
    
        /* the easiest case: we are in linux */
        size_t buff_len;
        if (buff_len = readlink ("/proc/self/exe", path, dest_len - 1) != -1)
        {
            path [buff_len] = '\0';
            dirname (path);
            strcat  (path, "/");
            return path;
        }
    
        /* Ups... not in linux, no  guarantee */
    
        /* check if we have something like execve("foobar", NULL, NULL) */
        if (argv0 == NULL)
        {
            /* we surrender and give current path instead */
            if (getcwd (path, dest_len) == NULL) return NULL;
            strcat  (path, "/");
            return path;
        }
    
    
        /* argv[0] */
        /* if dest_len < PATH_MAX may cause buffer overflow */
        if ((realpath (argv0, path)) && (!access (path, F_OK)))
        {
            dirname (path);
            strcat  (path, "/");
            return path;
        }
    
        /* Current path */
        baseName = basename (argv0);
        if (getcwd (path, dest_len - strlen (baseName) - 1) == NULL)
            return NULL;
    
        strcat (path, "/");
        strcat (path, baseName);
        if (access (path, F_OK) == 0)
        {
            dirname (path);
            strcat  (path, "/");
            return path;
        }
    
        /* Try the PATH. */
        systemPath = getenv ("PATH");
        if (systemPath != NULL)
        {
            dest_len--;
            systemPath = strdup (systemPath);
            for (candidateDir = strtok (systemPath, ":"); candidateDir != NULL; candidateDir = strtok (NULL, ":"))
            {
                strncpy (path, candidateDir, dest_len);
                strncat (path, "/", dest_len);
                strncat (path, baseName, dest_len);
    
                if (access(path, F_OK) == 0)
                {
                    free (systemPath);
                    dirname (path);
                    strcat  (path, "/");
                    return path;
                }
            }
            free(systemPath);
            dest_len++;
        }
    
        /* again someone has use execve: we dont knowe the executable name; we surrender and give instead current path */
        if (getcwd (path, dest_len - 1) == NULL) return NULL;
        strcat  (path, "/");
        return path;
    }
    

    EDITED: Fixed the bug reported by Mark lakata.

    0 讨论(0)
  • 2020-12-02 05:06

    There's no "guaranteed to work anywhere" method.

    Step 1 is to check argv[0], if the program was started by its full path, this would (usually) have the full path. If it was started by a relative path, the same holds (though this requires getting teh current working directory, using getcwd().

    Step 2, if none of the above holds, is to get the name of the program, then get the name of the program from argv[0], then get the user's PATH from the environment and go through that to see if there's a suitable executable binary with the same name.

    Note that argv[0] is set by the process that execs the program, so it is not 100% reliable.

    0 讨论(0)
  • 2020-12-02 05:06

    You can also get the path on GNU/Linux with (not thoroughly tested):

    char file[32];
    char buf[64];
    pid_t pid = getpid();
    sprintf(file, "/proc/%i/cmdline", pid);
    FILE *f = fopen(file, "r");
    fgets(buf, 64, f);
    fclose(f);
    

    If you want the directory of the executable for perhaps changing the working directory to the process's directory (for media/data/etc), you need to drop everything after the last /:

    *strrchr(buf, '/') = '\0';
    /*chdir(buf);*/
    
    0 讨论(0)
提交回复
热议问题