If PID 31340, a simple ps uww 31340
will tell you. ls -l /proc/31340
will show you more information, for example /proc/31340/exe
is a symbolic link to the program executable in case the name wasn't enough.
If PID 31340 isn't running anymore, it's too late unless you've installed some kind of logging software.
The simplest logging software for this job is acct
. Just install the package and it will start logging the name of all processes. The next time you want to know about some past PID, run
sudo dump-acct /var/log/account/pacct | grep 31340
For a more precise match of the last process that had this PID, you can use this incantation:
sudo dump-acct /var/log/account/pacct | awk -F '|' '$10 ~ /^ *31340 / {print; quit}'