The /proc directory
is actually a pseudo-filesystem. The files in /proc mirror currently running
system and kernel processes and contain
information and statistics about them.
CPU=$( awk '/model name/ {print $4}' < /proc/cpuinfo )
if [ $CPU = Pentium ]
then
run_some_commands
...
else
run_different_commands
...
fi
devfile="/proc/bus/usb/devices"
USB1="Spd=12"
USB2="Spd=480"
bus_speed=$(grep Spd $devfile | awk '{print $9}')
if [ "$bus_speed" = "$USB1" ]
then
echo "USB 1.1 port found."
# Do something appropriate for USB 1.1.
fi
The /proc directory
contains subdirectories with unusual numerical
names. Every one of these names maps to the process ID of a currently running
process. Within each of these subdirectories, there are
a number of files that hold useful information about the
corresponding process. The stat and
status files keep running statistics
on the process, the cmdline file holds
the command-line arguments the process was invoked with, and
the exe file is a symbolic link to the
complete path name of the invoking process. There are a few
more such files, but these seem to be the most interesting
from a scripting standpoint.
Example 27-2. Finding the process associated with a PID
#!/bin/bash
# pid-identifier.sh: Gives complete path name to process associated with pid.
ARGNO=1 # Number of arguments the script expects.
E_WRONGARGS=65
E_BADPID=66
E_NOSUCHPROCESS=67
E_NOPERMISSION=68
PROCFILE=exe
if [ $# -ne $ARGNO ]
then
echo "Usage: `basename $0` PID-number" >&2 # Error message >stderr.
exit $E_WRONGARGS
fi
pidno=$( ps ax | grep $1 | awk '{ print $1 }' | grep $1 )
# Checks for pid in "ps" listing, field #1.
# Then makes sure it is the actual process, not the process invoked by this script.
# The last "grep $1" filters out this possibility.
#
# pidno=$( ps ax | awk '{ print $1 }' | grep $1 )
# also works, as Teemu Huovila, points out.
if [ -z "$pidno" ] # If, after all the filtering, the result is a zero-length string,
then # no running process corresponds to the pid given.
echo "No such process running."
exit $E_NOSUCHPROCESS
fi
# Alternatively:
# if ! ps $1 > /dev/null 2>&1
# then # no running process corresponds to the pid given.
# echo "No such process running."
# exit $E_NOSUCHPROCESS
# fi
# To simplify the entire process, use "pidof".
if [ ! -r "/proc/$1/$PROCFILE" ] # Check for read permission.
then
echo "Process $1 running, but..."
echo "Can't get read permission on /proc/$1/$PROCFILE."
exit $E_NOPERMISSION # Ordinary user can't access some files in /proc.
fi
# The last two tests may be replaced by:
# if ! kill -0 $1 > /dev/null 2>&1 # '0' is not a signal, but
# this will test whether it is possible
# to send a signal to the process.
# then echo "PID doesn't exist or you're not its owner" >&2
# exit $E_BADPID
# fi
exe_file=$( ls -l /proc/$1 | grep "exe" | awk '{ print $11 }' )
# Or exe_file=$( ls -l /proc/$1/exe | awk '{print $11}' )
#
# /proc/pid-number/exe is a symbolic link
# to the complete path name of the invoking process.
if [ -e "$exe_file" ] # If /proc/pid-number/exe exists...
then # the corresponding process exists.
echo "Process #$1 invoked by $exe_file."
else
echo "No such process running."
fi
# This elaborate script can *almost* be replaced by
# ps ax | grep $1 | awk '{ print $5 }'
# However, this will not work...
# because the fifth field of 'ps' is argv[0] of the process,
# not the executable file path.
#
# However, either of the following would work.
# find /proc/$1/exe -printf '%l\n'
# lsof -aFn -p $1 -d txt | sed -ne 's/^n//p'
# Additional commentary by Stephane Chazelas.
exit 0
Example 27-3. On-line connect status
#!/bin/bash
PROCNAME=pppd # ppp daemon
PROCFILENAME=status # Where to look.
NOTCONNECTED=65
INTERVAL=2 # Update every 2 seconds.
pidno=$( ps ax | grep -v "ps ax" | grep -v grep | grep $PROCNAME | awk '{ print $1 }' )
# Finding the process number of 'pppd', the 'ppp daemon'.
# Have to filter out the process lines generated by the search itself.
#
# However, as Oleg Philon points out,
#+ this could have been considerably simplified by using "pidof".
# pidno=$( pidof $PROCNAME )
#
# Moral of the story:
#+ When a command sequence gets too complex, look for a shortcut.
if [ -z "$pidno" ] # If no pid, then process is not running.
then
echo "Not connected."
exit $NOTCONNECTED
else
echo "Connected."; echo
fi
while [ true ] # Endless loop, script can be improved here.
do
if [ ! -e "/proc/$pidno/$PROCFILENAME" ]
# While process running, then "status" file exists.
then
echo "Disconnected."
exit $NOTCONNECTED
fi
netstat -s | grep "packets received" # Get some connect statistics.
netstat -s | grep "packets delivered"
sleep $INTERVAL
echo; echo
done
exit 0
# As it stands, this script must be terminated with a Control-C.
# Exercises:
# ---------
# Improve the script so it exits on a "q" keystroke.
# Make the script more user-friendly in other ways.
In general, it is dangerous to
write to the files in /proc, as this can corrupt the
filesystem or crash the machine.