29.2. /proc
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.
bash$ cat /proc/devices
Character devices:
1 mem
2 pty
3 ttyp
4 ttyS
5 cua
7 vcs
10 misc
14 sound
29 fb
36 netlink
128 ptm
136 pts
162 raw
254 pcmcia
Block devices:
1 ramdisk
2 fd
3 ide0
9 md
bash$ cat /proc/interrupts
CPU0
0: 84505 XT-PIC timer
1: 3375 XT-PIC keyboard
2: 0 XT-PIC cascade
5: 1 XT-PIC soundblaster
8: 1 XT-PIC rtc
12: 4231 XT-PIC PS/2 Mouse
14: 109373 XT-PIC ide0
NMI: 0
ERR: 0
bash$ cat /proc/partitions
major minor #blocks name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq
3 0 3007872 hda 4472 22260 114520 94240 3551 18703 50384 549710 0 111550 644030
3 1 52416 hda1 27 395 844 960 4 2 14 180 0 800 1140
3 2 1 hda2 0 0 0 0 0 0 0 0 0 0 0
3 4 165280 hda4 10 0 20 210 0 0 0 0 0 210 210
...
bash$ cat /proc/loadavg
0.13 0.42 0.27 2/44 1119
bash$ cat /proc/apm
1.16 1.2 0x03 0x01 0xff 0x80 -1% -1 ?
bash$ cat /proc/acpi/battery/BAT0/info
present: yes
design capacity: 43200 mWh
last full capacity: 36640 mWh
battery technology: rechargeable
design voltage: 10800 mV
design capacity warning: 1832 mWh
design capacity low: 200 mWh
capacity granularity 1: 1 mWh
capacity granularity 2: 1 mWh
model number: IBM-02K6897
serial number: 1133
battery type: LION
OEM info: Panasonic
bash$ fgrep Mem /proc/meminfo
MemTotal: 515216 kB
MemFree: 266248 kB
|
Shell scripts may extract data from certain of the files in /proc.
FS=iso
grep $FS /proc/filesystems
|
kernel_version=$( awk '{ print $3 }' /proc/version )
|
CPU=$( awk '/model name/ {print $5}' < /proc/cpuinfo )
if [ "$CPU" = "Pentium(R)" ]
then
run_some_commands
...
else
run_other_commands
...
fi
cpu_speed=$( fgrep "cpu MHz" /proc/cpuinfo | awk '{print $4}' )
|
#!/bin/bash
OPTION=cmdline
pid=$( echo $(pidof "$1") | awk '{ print $1 }' )
echo
echo "Process ID of (first instance of) "$1" = $pid"
echo -n "Command-line arguments: "
cat /proc/"$pid"/"$OPTION" | xargs -0 echo
echo; echo
|
+
devfile="/proc/bus/usb/devices"
text="Spd"
USB1="Spd=12"
USB2="Spd=480"
bus_speed=$(fgrep -m 1 "$text" $devfile | awk '{print $9}')
if [ "$bus_speed" = "$USB1" ]
then
echo "USB 1.1 port found."
fi
|
|
It is even possible to control certain peripherals with commands sent to the /proc directory.
root# echo on > /proc/acpi/ibm/light
|
This turns on the Thinklight in certain models of IBM/Lenovo Thinkpads. (May not work on all Linux distros.)
Of course, caution is advised when writing to /proc.
|
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 29-3. Finding the process associated with a PID
#!/bin/bash
ARGNO=1
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
exit $E_WRONGARGS
fi
pidno=$( ps ax | grep $1 | awk '{ print $1 }' | grep $1 )
if [ -z "$pidno" ]
then
echo "No such process running."
exit $E_NOSUCHPROCESS
fi
if [ ! -r "/proc/$1/$PROCFILE" ]
then
echo "Process $1 running, but..."
echo "Can't get read permission on /proc/$1/$PROCFILE."
exit $E_NOPERMISSION
fi
exe_file=$( ls -l /proc/$1 | grep "exe" | awk '{ print $11 }' )
if [ -e "$exe_file" ]
then
echo "Process #$1 invoked by $exe_file."
else
echo "No such process running."
fi
exit 0
|
Example 29-4. On-line connect status
#!/bin/bash
PROCNAME=pppd
PROCFILENAME=status
NOTCONNECTED=85
INTERVAL=2
pidno=$( ps ax | grep -v "ps ax" | grep -v grep | grep $PROCNAME |
awk '{ print $1 }' )
if [ -z "$pidno" ]
then
echo "Not connected."
else
echo "Connected."; echo
fi
while [ true ]
do
if [ ! -e "/proc/$pidno/$PROCFILENAME" ]
then
echo "Disconnected."
fi
netstat -s | grep "packets received"
netstat -s | grep "packets delivered"
sleep $INTERVAL
echo; echo
done
exit 0
|
|
In general, it is dangerous to write to the files in /proc, as this can corrupt the filesystem or crash the machine.
|