Domanda

In ambiente Windows esiste un'API per ottenere il percorso che sta eseguendo un processo. C'è qualcosa di simile in Unix / Linux?

O c'è un altro modo per farlo in questi ambienti?

È stato utile?

Soluzione

Su Linux, il collegamento simbolico /proc/<pid>/exe ha il percorso dell'eseguibile. Utilizzare il comando readlink -f /proc/<pid>/exe per ottenere il valore.

Su AIX, questo file non esiste. Puoi confrontare cksum <actual path to binary> e cksum /proc/<pid>/object/a.out.

Altri suggerimenti

Puoi trovare facilmente l'exe in questi modi, provalo tu stesso.

  • ll /proc/<PID>/exe
  • pwdx <PID>
  • lsof -p <PID> | grep cwd

Un po 'in ritardo, ma tutte le risposte erano specifiche per Linux.

Se è necessario anche unix, è necessario questo:

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: corretto l'errore segnalato da Mark lakata.

Uso:

ps -ef | grep 786

Sostituisci 786 con il tuo PID o il nome del processo.

pwdx <process id>

Questo comando prenderà il percorso del processo da dove viene eseguito.

In Linux ogni processo ha la sua cartella in /proc. Quindi puoi usare getpid() per ottenere il pid del processo in esecuzione e poi unirti a esso con il percorso <=> per ottenere la cartella che speriamo ti serva.

Ecco un breve esempio in Python:

import os
print os.path.join('/proc', str(os.getpid()))

Ecco anche l'esempio in ANSI C:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>


int
main(int argc, char **argv)
{
    pid_t pid = getpid();

    fprintf(stdout, "Path to current process: '/proc/%d/'\n", (int)pid);

    return EXIT_SUCCESS;
}

Compilalo con:

gcc -Wall -Werror -g -ansi -pedantic process_path.c -oprocess_path 

Non c'è " garantito per funzionare ovunque " metodo.

Il passo 1 è controllare argv [0], se il programma è stato avviato con il suo percorso completo, questo (di solito) avrebbe il percorso completo. Se è stato avviato da un percorso relativo, lo stesso vale (sebbene ciò richieda ottenere la directory di lavoro corrente, usando getcwd ().

Il passaggio 2, se nessuna delle precedenti è valido, è ottenere il nome del programma, quindi ottenere il nome del programma da argv [0], quindi ottenere il PERCORSO dell'utente dall'ambiente e passare attraverso quello per vedere se c'è un binario eseguibile adatto con lo stesso nome.

Nota che argv [0] è impostato dal processo che esegue il programma, quindi non è affidabile al 100%.

grazie:     Kiwy
con AIX:

getPathByPid()
{
    if [[ -e /proc/$1/object/a.out ]]; then
        inode=`ls -i /proc/$1/object/a.out 2>/dev/null | awk '{print $1}'`
        if [[ $? -eq 0 ]]; then
            strnode=${inode}"$"
            strNum=`ls -li /proc/$1/object/ 2>/dev/null | grep $strnode | awk '{print $NF}' | grep "[0-9]\{1,\}\.[0-9]\{1,\}\."`
            if [[ $? -eq 0 ]]; then
                # jfs2.10.6.5869
                n1=`echo $strNum|awk -F"." '{print $2}'`
                n2=`echo $strNum|awk -F"." '{print $3}'`
                # brw-rw----    1 root     system       10,  6 Aug 23 2013  hd9var
                strexp="^b.*"$n1,"[[:space:]]\{1,\}"$n2"[[:space:]]\{1,\}.*$"   # "^b.*10, \{1,\}5 \{1,\}.*$"
                strdf=`ls -l /dev/ | grep $strexp | awk '{print $NF}'`
                if [[ $? -eq 0 ]]; then
                    strMpath=`df | grep $strdf | awk '{print $NF}'`
                    if [[ $? -eq 0 ]]; then
                        find $strMpath -inum $inode 2>/dev/null
                        if [[ $? -eq 0 ]]; then
                            return 0
                        fi
                    fi
                fi
            fi
        fi
    fi
    return 1
}

Puoi anche ottenere il percorso su GNU / Linux con (non accuratamente testato):

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);

Se si desidera che la directory dell'eseguibile possa cambiare la directory di lavoro nella directory del processo (per media / dati / ecc.), è necessario eliminare tutto dopo l'ultimo /:

*strrchr(buf, '/') = '\0';
/*chdir(buf);*/

Il comando seguente cerca il nome del processo nell'elenco dei processi in esecuzione e reindirizza il comando pid su pwdx per trovare la posizione del processo.

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

Sostituisci " abc " con il tuo modello specifico.

In alternativa, se è possibile configurarlo come funzione in .bashrc, potrebbe essere utile usarlo se è necessario utilizzarlo frequentemente.

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

Ad esempio:

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

18404: /home2/Avro/NIFI

Spero che questo aiuti qualcuno a volte .....

Trova il percorso di un nome processo

#!/bin/bash
# @author Lukas Gottschall
PID=`ps aux | grep precessname | grep -v grep | awk '{ print $2 }'`
PATH=`ls -ald --color=never /proc/$PID/exe | awk '{ print $10 }'`
echo $PATH
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top