Question

How can I find the PID of a running process using ls in bash? Let's assume I know the name of the process. Using ps would be easy. If I were to search for firefox, it would simply be ps ax | grep firefox. However for arguments sake, let's say I can't do that and I have to use ls.

Any help would be great!

Was it helpful?

Solution

The /proc file system has a directory entry for each process. Each directory contains a file that is a symbolic link to the executable. If you can filter the list with grep for example you can get the PID from the path. For example:

$ ls -l /proc/*/exe | grep firefox
lrwxrwxrwx 1 joni     joni     0 Feb  9 01:39 /proc/8321/exe -> /usr/lib/firefox/firefox
lrwxrwxrwx 1 joni     joni     0 Feb  9 00:09 /proc/8825/exe -> /usr/lib/firefox/plugin-container

OTHER TIPS

you can do the following solution: :)

ls -l /proc/*/exe | grep firefox | cut -d/ -f3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top