Re: retrieve absolute path from pid (732 Views)
Reply
Occasional Visitor
Senthilprakash_1
Posts: 4
Registered: ‎04-28-2004
Message 1 of 16 (732 Views)
Accepted Solution

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.
Please use plain text.
Honored Contributor
Robert-Jan Goossens
Posts: 7,384
Registered: ‎04-04-2000
Message 2 of 16 (732 Views)

Re: retrieve absolute path from pid

Hi Prakash,

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

Regards,
Robert-Jan
Please use plain text.
Respected Contributor
Franky_1
Posts: 322
Registered: ‎01-14-2001
Message 3 of 16 (732 Views)

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
Please use plain text.
Honored Contributor
Bharat Katkar
Posts: 2,309
Registered: ‎05-29-2003
Message 4 of 16 (732 Views)

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
Please use plain text.
Occasional Visitor
Senthilprakash_1
Posts: 4
Registered: ‎04-28-2004
Message 5 of 16 (732 Views)

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
Please use plain text.
Honored Contributor
Robert-Jan Goossens
Posts: 7,384
Registered: ‎04-04-2000
Message 6 of 16 (732 Views)

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.
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 7 of 16 (732 Views)

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!
Please use plain text.
Valued Contributor
Petr Simik_1
Posts: 121
Registered: ‎10-22-2002
Message 8 of 16 (732 Views)

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.
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 9 of 16 (732 Views)

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).
Please use plain text.
Occasional Visitor
Senthilprakash_1
Posts: 4
Registered: ‎04-28-2004
Message 10 of 16 (732 Views)

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.
Please use plain text.
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 11 of 16 (732 Views)

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
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,750
Registered: ‎06-21-2000
Message 12 of 16 (732 Views)

Re: retrieve absolute path from pid

Bill said it all. The full path is only stored if the process is started that way.
Please use plain text.
Esteemed Contributor
ranganath ramachandra
Posts: 375
Registered: ‎05-16-2003
Message 13 of 16 (732 Views)

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);
}
---
Please use plain text.
Esteemed Contributor
ranganath ramachandra
Posts: 375
Registered: ‎05-16-2003
Message 14 of 16 (732 Views)

Re: retrieve absolute path from pid

oops i meant to say 11.11 and 11.23 but not on 11.00.
Please use plain text.
Occasional Visitor
Senthilprakash_1
Posts: 4
Registered: ‎04-28-2004
Message 15 of 16 (732 Views)

Re: retrieve absolute path from pid

Thanks, the program works :)
Please use plain text.
Respected Contributor
Prashant Zanwar_4
Posts: 772
Registered: ‎04-30-2004
Message 16 of 16 (732 Views)

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."
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation