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

后端 未结 11 640
无人共我
无人共我 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:54

    thanks : Kiwy
    with 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
    }
    

提交回复
热议问题