kxix
asked on
Full path name of process command
How do I get the full path name of the command used to start a process ?
(in a shell script)
Specifically a process might be started as "./xxx" where xxx is the binary.
No matter what version of ps I try it will not reveal to me where the binary
resides.
I've tried for example:
/usr/ucb/ps -auxwwwl
ps -efd
I also tried using /usr/proc/pmap <pid> but this command will only tell me the
full path of library files used by the process, not the binary itself.
Any ideas ?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To find out the FULL path of the command, you need to use: /usr/bin/ps with -o args,
eg:
/usr/bin/ps -e -o pid,args | grep mycommand
man ps
to learn more details.
eg:
/usr/bin/ps -e -o pid,args | grep mycommand
man ps
to learn more details.
ASKER
Reply to yuzh: Nope, does not return the full path name of the binary if the
process was started from the directory where the binary resides.
process was started from the directory where the binary resides.
That's the limit of ps, but it does tell the story about how the process was start!
When you run something like:
./ls
it show you ./ls
/bin/ls
it give you
/bin/ls
When you run something like:
./ls
it show you ./ls
/bin/ls
it give you
/bin/ls
You can't expect the world. If you run a process such as ./ls then how is the kernel supposed to remember where you started it from in the future?
Either get into the habit of running all your processes with their full path eg /usr/bin/ls or put up with it.
You can of course "find / -name "ls" -ls" and see how many binaries you have and where it is of the indentified process.
Then use your memory to remember which one you ran ;)
Either get into the habit of running all your processes with their full path eg /usr/bin/ls or put up with it.
You can of course "find / -name "ls" -ls" and see how many binaries you have and where it is of the indentified process.
Then use your memory to remember which one you ran ;)
Comments from matjc are entirely correct, the newer implementation of the /proc filesystem has taken away a few things we used to take for granted, and to be fair its tough to ask the OS to store the absolute path with every process when 1000s are started/dropped in every minute.
However, dont give up - here's a couple of 'B' grade answers because I'm in a bit of a hurry:
1. Try the UCB ps command
If you have /usr/ucb/ps installed (or /ucb/ps) you could try that version of ps. Its not truly supported for Solaris 9 and later, but a lot of corporations put a lot of heat on Sun to make sure it is still available to support older scripts. If I'm right, you can use:
/usr/ucb/ps -auxwww
If this doesn't work, look around for the options available to that version of the ps command.
2. Use the pfiles tools to determine the environment under which the process was run.
If you do a 'man pfiles' I believe there is one command that can show you the working environment when the process was started. You should be able to use the pfiles tools to find
Using this tool, you should be able to work out the command line (CMD), the Current Working Directory (CWD) and PATH. You can then write a simple script to do the following:
If $CMD matches "./binary",
then
the path is "$CWD/binary".
If the process was started without any '/' characters,
then
set path to $PATH
which "$CMD"
Note that I just used the variable names to shorten this text, you'll have to make up your own :-)
However, dont give up - here's a couple of 'B' grade answers because I'm in a bit of a hurry:
1. Try the UCB ps command
If you have /usr/ucb/ps installed (or /ucb/ps) you could try that version of ps. Its not truly supported for Solaris 9 and later, but a lot of corporations put a lot of heat on Sun to make sure it is still available to support older scripts. If I'm right, you can use:
/usr/ucb/ps -auxwww
If this doesn't work, look around for the options available to that version of the ps command.
2. Use the pfiles tools to determine the environment under which the process was run.
If you do a 'man pfiles' I believe there is one command that can show you the working environment when the process was started. You should be able to use the pfiles tools to find
Using this tool, you should be able to work out the command line (CMD), the Current Working Directory (CWD) and PATH. You can then write a simple script to do the following:
If $CMD matches "./binary",
then
the path is "$CWD/binary".
If the process was started without any '/' characters,
then
set path to $PATH
which "$CMD"
Note that I just used the variable names to shorten this text, you'll have to make up your own :-)
ASKER