Hi,
I am looking for a method to get a process-executable-name from a pid. Any
ideas?
Best regards
Stefan
I am looking for a method to get a process-executable-name from a pid. Any
ideas?
Best regards
Stefan
read /proc/pid/psinfo
The actual executable itself can be found as:
/proc/pid/object/a.out
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
If you're talking about doing this from C code:
system("ps -o pid -o comm | grep 1367 | cut -d' ' -f3 > outifle");
then open and read the file "outfile" (might work...)
Good luck,
Tim
> I am looking for a method to get a process-executable-name from a pid. Any
> ideas?
> Best regards
> Stefan
if you know the pid why would you grep a complete list for it? just haveQuote:>If you're working on the command line, try the following:
>ps -o pid -o comm | grep PID | cut -d' ' -f3
>where PID is the known pid
ps -h -o comm -p PID
it's easier and somewhat safer to use popen, e.g.,Quote:>If you're talking about doing this from C code:
>system("ps -o pid -o comm | grep 1367 | cut -d' ' -f3 > outifle");
>then open and read the file "outfile" (might work...)
char buf[100] = {0};
FILE *p = popen("ps -h -o comm -p 1367", "r");
if (NULL == p) perror("popen");
else {
if (NULL == fgets(buf,sizeof buf,p)) if (0 != errno) perror("fgets");
pclose(p);
}
it would be much safer to use exec, but there's more code you have to write
to handle it, e.g.,
char buf[100] = {0}; int p[2];
if (0 > pipe(p)) perror("pipe");
else {
pid_t pid = fork();
if ((pid_t)0 > pid) perror("fork");
else if ((pid_t)0 == pid) {
if (0 > dup2(p[1],STDOUT_FILENO)) perror("dup2");
else execl("/bin/ps","ps","-h","-o","comm","-p","1367",NULL);
_exit(127);
}
else {
pid_t n;
do n = waitpid(pid,NULL,0); while ((pid_t)0 > n && EINTR == errno);
if (n != pid) error("waitpid");
else {
FILE *f = fdopen(p[0],"r");
if (NULL == f) perror("fdopen");
else {
if (NULL == fgets(buf,sizeof buf,f)) if (0 != errno) perror("fgets");
if (0 > fclose(f)) perror("fclose");
}
}
}
close(p[0]); close(p[1]);
}
note: i've excluded some stuff, such as signal handling and closing unused
pipe fd's, but is the basic form.
--
okay, have a sig then
1. Getting the executable name.
Hi all,
What's the best way to get the executable name of a program ?
I mean :
int main (int argc, char *argv[])
{
printf("myname is: %s\n", argv[0]);
return 0;
This is ok but if the program is executed like this ./program
or /usr/mysuperlong/path/program the output wouldn't be
the executable name only (path is included).
So how could I do ?
Thanks.
2. "Serial Line is Looped Back". ?
4. connecting to remote windows machines via samba ?
5. Getting the executable's name without using argv[0]
6. About Pinnacle PCTV support for Linux
7. Getting executable name of a running process
9. Getting children's PID with parent's pid?
10. Named - Not writing named.pid anymore
11. Which named ?...Solaris 2.5 in.named or Berkley named ?
12. getting full domain name from Solaris 2.4
13. How to make a RSH and getting is PID.