Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

retrieve absolute path from pid

SOLVED
Go to solution
Senthilprakash_1
Occasional Visitor

retrieve absolute path from pid

hi,

I have the pid of the process. Using this pid i should retrieve the absolute path name of the executable. I tried pstat_getproc(), but it gives only the basename of the executable. I need the full path. Is there any system call available in HP-UX to do that or can i do this using scripting?

Thanks,
Prakash.
15 REPLIES
Robert-Jan Goossens
Honored Contributor

Re: retrieve absolute path from pid

Hi Prakash,

# UNIX95= ps -e -o ruser,pid,vsz,args

Regards,
Robert-Jan
Franky_1
Respected Contributor

Re: retrieve absolute path from pid

Hi,

you can user the "UNIX95" option

eg

UNIX95= ps -eo time,vsz,pid,args

(see "columns" from man ps for more options)
and be aware of the " " before ps
This should give you what you want

Regards

Franky
Don't worry be happy
Bharat Katkar
Honored Contributor

Re: retrieve absolute path from pid

HI Prakash,
How about this one.

# PIDNO=xxxx
# ps -ef | grep $PIDNO | grep -v grep | awk '{ print $9 }'

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Senthilprakash_1
Occasional Visitor

Re: retrieve absolute path from pid

hi,

I invoke a executable ./a.out in /tmp/one.
Now i am in some different directory and try using the script provided i still get as
./a.out and not as /tmp/one/a.out.

Prakash
Robert-Jan Goossens
Honored Contributor

Re: retrieve absolute path from pid

you wont, the process is being started as ./a.out ( no full path )

if you start is as # /tmp/one/a.out (without the .)the full path wil be visible in the ps list.
Muthukumar_5
Honored Contributor

Re: retrieve absolute path from pid

I hope you are expecting full path and execution of a process there. We can do in linux using /proc filesystem. Every process ID contains exe part which gives link to exact execution path there.

We can not get it using ps command there. We can get basename of execution, if we execute them as only that command. We can full path name when we does with full path name.

You can go to glance tool, to know more about a process there. I am not sure which will give or not.

HTH.
Easy to suggest when don't know about the problem!
Petr Simik_1
Valued Contributor

Re: retrieve absolute path from pid

You cann't get PATH from ps everytime.

PATH output from ps is limited to 80 characters. If the PATH is longer you don't get it.

I am curious if anybody know how to get full PATH.
Bill Hassell
Honored Contributor

Re: retrieve absolute path from pid

The full path is not stored in the process table unless the process is started with a full path (ie, starts with /). You can see the full path and the parameters (very common request for Java stuff) by using -x as in:

ps -efx

NOTE: -x is *only* available for patched versions of ps (11.00 and 11i only).


Bill Hassell, sysadmin
Senthilprakash_1
Occasional Visitor

Re: retrieve absolute path from pid

If I wont be able to retrieve the full pathname using "ps" then is there any system call and work around which will get me abolsute path using pid.

Thanks,
Prakash.
RAC_1
Honored Contributor

Re: retrieve absolute path from pid

Finally Bill talked about option -x for ps command. You need to have ps patch for it. Else you can use UNIX95 command. Also as explained earlier by few memebers, if you started the progrm with absolute path, you will get it, else not.

ps -efx|grep "xyz"
UNIX95= ps -ef -o "ruser,args,pid" | grep "xyz"

Anil
There is no substitute to HARDWORK
Patrick Wallek
Honored Contributor

Re: retrieve absolute path from pid

Bill said it all. The full path is only stored if the process is started that way.
ranganath ramachandra
Esteemed Contributor
Solution

Re: retrieve absolute path from pid

i got the filename from the program below, on 11.00, 11.11 and 11.23.

---
#include
#include

int main (int argc, char *argv[])
{
struct pst_vm_status vmstat;
struct pst_filedetails psfdetails;
char name[1024];
int ret;
int pid = atoi(argv[1]);

/* last parameter to getprocvm is 1 */
/* because it worked :P will confirm */
/* with vm folks later */
if ((ret = pstat_getprocvm(&vmstat, sizeof(vmstat), pid, 1)) <0) {
perror("pstat_getprocvm");
exit(10);
}

if ((ret=pstat_getfiledetails(&psfdetails, sizeof(psfdetails),&(vmstat.pst_fid))) <0) {
perror("pstat_getfiledetails");
exit(20);
}

// printf ("device: %d, inode: %u\n", psfdetails.psfd_rdev, psfdetails.psfd_ino);

if ((ret=pstat_getpathname(name, 256, &(vmstat.pst_fid))) <0) {
perror("pstat_getpathname");
exit(30);
}

puts(name);
}
---
ranganath ramachandra
Esteemed Contributor

Re: retrieve absolute path from pid

oops i meant to say 11.11 and 11.23 but not on 11.00.
Senthilprakash_1
Occasional Visitor

Re: retrieve absolute path from pid

Thanks, the program works :)
Highlighted
Prashant Zanwar_4
Respected Contributor

Re: retrieve absolute path from pid


This is to track down the childs.

Hope it helps..

/bin/ps -ef | awk -v v= ' ($3 == v) { print $2 } '

Thanks
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."