A friend of mine recently asked me about finding the absolute command path of a process given the pid. Ofcourse, it’s very easy to do it for a particular OS. Doing it platform independently is actually a little tough. There’s no straight-forward POSIXy way of doing this (as far as I know). One can certainly do some /proc magic, but that won’t be portable. My suggestion was to just use ps. This works for both Solaris and Linux. So, you get the output from

ps -p <pid> -f

UID   PID  PPID  C    STIME TTY      TIME CMD
draganm 17198 17193  0 20:40:40 ?        0:00 csh -c /usr/lib/ssh/sftp-server

and simply do some string manipulation to get the required string (shown in bold above). Any other ideas folks?

Share