Even though /proc
has been mentioned briefly above as a source of information for learning about processes on a system, I include here a basic Bash script to parse some information out of it. For an excellent introduction to the /proc
virtual filesystem, see this IBM article, which explains how it can be useful for both users and programmers.
Parsing /proc
with a generic script can be unreliable, as different processes often have different /proc
layouts, but basic information can usually be gleaned with this script, although it may have to be modified if you want to parse any other specific information. I have written it using Bash, but there are better parsers which you could investigate if you want to write it using a different scripting language.
Often in the output of the script you may see a "permission denied" message, which means the /proc
location is not readable by $USER, so you can run the script as root if you want in that case to gain the full output.
Copy the body of the script into a new file, save it, and make it executable (chmod u+x
) and then call it with at least one argument (it now can handle multiple processes at the same time):
./proc_script xfce4-panel
or put it in your $HOME/bin
and then you can run it like other programs if $HOME/bin
is in your path. (However, if you use sudo you will still have to give the absolute path location of the script, as $HOME/bin
is not in root's path.)
The body of the script; it is also available and updated at my Github page:
#!/usr/bin/env bash
(( $# == 0 )) && { printf "Usage: Please specify the name of one process \
to research.\n" >&2; exit 1; }
args=("$@")
for process in "${args[@]}"; do
# store user input in a variable and warn then exit if program is not running
queried_pid="$(pgrep "${process}" || { printf '%s\n' "No such program" >&2; })"
# add the contents of the variable into an array
pidarray=($(echo "$queried_pid"))
# iterate over the array, however many pids have been found
for i in "${pidarray[@]}"; do
printf "Pid is: %s\nExe is: %s\nCmdline is: %s\ncwd is: %s\n" "$(echo "$i")" \
"$(stat -c %N /proc/"$i"/exe)" "$(cat /proc/"$i"/cmdline)" "$(cd /proc/"$i"/cwd/; pwd -P)"
done
printf "\n\n"
done
exit
Sample output 1:
./proc_parse firefox
Pid is: 1830
Exe is: `/proc/1830/exe' -> `/usr/lib/firefox/firefox'
Cmdline is: /usr/lib/firefox/firefox
cwd is: /home/mike
Sample Output 2 (dealing with multiple pids):
./proc_parse evince
Pid is: 3113
Exe is: `/proc/3113/exe' -> `/usr/bin/evince'
Cmdline is: evince/home/mike/Z_IBM_lpic_Linux_pdfs/l-proc-pdf.pdf
cwd is: /home/mike
Pid is: 3119
Exe is: `/proc/3119/exe' -> `/usr/lib/evince/evinced'
Cmdline is: /usr/lib/evince/evinced
cwd is: /