old-www/LDP/abs/html/procref1.html

885 lines
14 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>/proc</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="/dev and /proc"
HREF="devproc.html"><LINK
REL="PREVIOUS"
TITLE="/dev"
HREF="devref1.html"><LINK
REL="NEXT"
TITLE="Network Programming"
HREF="networkprogramming.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="devref1.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 29. <TT
CLASS="FILENAME"
>/dev</TT
> and <TT
CLASS="FILENAME"
>/proc</TT
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="networkprogramming.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="PROCREF1"
></A
>29.2. <TT
CLASS="FILENAME"
>/proc</TT
></H1
><P
><A
NAME="PROCREF2"
></A
></P
><P
>The <TT
CLASS="FILENAME"
>/proc</TT
> directory
is actually a pseudo-filesystem. The files in <TT
CLASS="FILENAME"
>/proc</TT
> mirror currently running
system and kernel <A
HREF="special-chars.html#PROCESSREF"
>processes</A
>
and contain information and statistics about them.</P
><P
> <TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
><TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat /proc/devices</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>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</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat /proc/interrupts</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
> 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</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat /proc/partitions</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>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
...</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat /proc/loadavg</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>0.13 0.42 0.27 2/44 1119</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat /proc/apm</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>1.16 1.2 0x03 0x01 0xff 0x80 -1% -1 ?</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat /proc/acpi/battery/BAT0/info</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>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</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>fgrep Mem /proc/meminfo</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>MemTotal: 515216 kB
MemFree: 266248 kB</TT
>
</PRE
></FONT
></TD
></TR
></TABLE
>
</P
><P
>Shell scripts may extract data from certain of the files in
<TT
CLASS="FILENAME"
>/proc</TT
>.
<A
NAME="AEN19180"
HREF="#FTN.AEN19180"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>FS=iso # ISO filesystem support in kernel?
grep $FS /proc/filesystems # iso9660</PRE
></FONT
></TD
></TR
></TABLE
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>kernel_version=$( awk '{ print $3 }' /proc/version )</PRE
></FONT
></TD
></TR
></TABLE
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>CPU=$( awk '/model name/ {print $5}' &#60; /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}' )
# Current operating speed (in MHz) of the cpu on your machine.
# On a laptop this may vary, depending on use of battery
#+ or AC power.</PRE
></FONT
></TD
></TR
></TABLE
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>#!/bin/bash
# get-commandline.sh
# Get the command-line parameters of a process.
OPTION=cmdline
# Identify PID.
pid=$( echo $(pidof "$1") | awk '{ print $1 }' )
# Get only first ^^^^^^^^^^^^^^^^^^ of multiple instances.
echo
echo "Process ID of (first instance of) "$1" = $pid"
echo -n "Command-line arguments: "
cat /proc/"$pid"/"$OPTION" | xargs -0 echo
# Formats output: ^^^^^^^^^^^^^^^
# (Thanks, Han Holl, for the fixup!)
echo; echo
# For example:
# sh get-commandline.sh xterm</PRE
></FONT
></TD
></TR
></TABLE
></P
><P
>+</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>devfile="/proc/bus/usb/devices"
text="Spd"
USB1="Spd=12"
USB2="Spd=480"
bus_speed=$(fgrep -m 1 "$text" $devfile | awk '{print $9}')
# ^^^^ Stop after first match.
if [ "$bus_speed" = "$USB1" ]
then
echo "USB 1.1 port found."
# Do something appropriate for USB 1.1.
fi</PRE
></FONT
></TD
></TR
></TABLE
></P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>It is even possible to control certain peripherals with commands
sent to the <TT
CLASS="FILENAME"
>/proc</TT
> directory.
<TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>root# </TT
><B
CLASS="COMMAND"
>echo on &#62; /proc/acpi/ibm/light</B
>
</PRE
></FONT
></TD
></TR
></TABLE
>
This turns on the <EM
>Thinklight</EM
> in certain models
of IBM/Lenovo Thinkpads. (May not work on all Linux distros.)</P
><P
>Of course, caution is advised when writing to <TT
CLASS="FILENAME"
>/proc</TT
>.</P
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="PROCRUNNING"
></A
></P
><P
>The <TT
CLASS="FILENAME"
>/proc</TT
> directory
contains subdirectories with unusual numerical
names. Every one of these names maps to the <A
HREF="internalvariables.html#PPIDREF"
>process ID</A
> 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 <TT
CLASS="FILENAME"
>stat</TT
> and
<TT
CLASS="FILENAME"
>status</TT
> files keep running statistics
on the process, the <TT
CLASS="FILENAME"
>cmdline</TT
> file holds
the command-line arguments the process was invoked with, and
the <TT
CLASS="FILENAME"
>exe</TT
> 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.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="PIDID"
></A
><P
><B
>Example 29-3. Finding the process associated with a PID</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>#!/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" &#62;&#38;2 # Error message &#62;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 &#62; /dev/null 2&#62;&#38;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 &#62; /dev/null 2&#62;&#38;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" &#62;&#38;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 #+ 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</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><A
NAME="CONSTAT"
></A
><P
><B
>Example 29-4. On-line connect status</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>#!/bin/bash
# connect-stat.sh
# Note that this script may need modification
#+ to work with a wireless connection.
PROCNAME=pppd # ppp daemon
PROCFILENAME=status # Where to look.
NOTCONNECTED=85
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.
# Fix the script to work with wireless/DSL connections.</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="PROCWARNING"
></A
></P
><DIV
CLASS="WARNING"
><P
></P
><TABLE
CLASS="WARNING"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/warning.gif"
HSPACE="5"
ALT="Warning"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>In general, it is dangerous to
<EM
>write</EM
> to the files in <TT
CLASS="FILENAME"
>/proc</TT
>, as this can corrupt the
filesystem or crash the machine.</P
></TD
></TR
></TABLE
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN19180"
HREF="procref1.html#AEN19180"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Certain system commands, such as
<A
HREF="system.html#PROCINFOREF"
>procinfo</A
>,
<A
HREF="system.html#FREEREF"
>free</A
>,
<A
HREF="system.html#VMSTATREF"
>vmstat</A
>,
<A
HREF="system.html#LSDEVREF"
>lsdev</A
>,
and <A
HREF="system.html#UPTIMEREF"
>uptime</A
>
do this as well.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="devref1.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="networkprogramming.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><TT
CLASS="FILENAME"
>/dev</TT
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="devproc.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Network Programming</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>