⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 procref1.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>/proc</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="/dev and /proc"HREF="devproc.html"><LINKREL="PREVIOUS"TITLE="/dev and /proc"HREF="devproc.html"><LINKREL="NEXT"TITLE="Of Zeros and Nulls"HREF="zeros.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="devproc.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 27. /dev and /proc</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="zeros.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="PROCREF1">27.2. <TTCLASS="FILENAME">/proc</TT></A></H1><P>The <TTCLASS="FILENAME">/proc</TT> directory	is actually a pseudo-filesystem.  The files in <TTCLASS="FILENAME">/proc</TT> mirror currently running	system and kernel <ICLASS="EMPHASIS">processes</I> and contain	information and statistics about them.</P><P>         <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat /proc/devices</B></TT> <TTCLASS="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>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat /proc/interrupts</B></TT> <TTCLASS="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>   <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat /proc/partitions</B></TT> <TTCLASS="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>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat /proc/loadavg</B></TT> <TTCLASS="COMPUTEROUTPUT">0.13 0.42 0.27 2/44 1119</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat /proc/apm</B></TT> <TTCLASS="COMPUTEROUTPUT">1.16 1.2 0x03 0x01 0xff 0x80 -1% -1 ?</TT>          </PRE></TD></TR></TABLE>      </P><P>Shell scripts may extract data from certain of the files in         <TTCLASS="FILENAME">/proc</TT>.	   <ANAME="AEN14673"HREF="#FTN.AEN14673">[1]</A></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;FS=iso                       # ISO filesystem support in kernel?   2&nbsp;   3&nbsp;grep $FS /proc/filesystems   # iso9660</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;kernel_version=$( awk '{ print $3 }' /proc/version )</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;CPU=$( awk '/model name/ {print $4}' &#60; /proc/cpuinfo )   2&nbsp;   3&nbsp;if [ $CPU = Pentium ]   4&nbsp;then   5&nbsp;  run_some_commands   6&nbsp;  ...   7&nbsp;else   8&nbsp;  run_different_commands   9&nbsp;  ...  10&nbsp;fi</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;devfile="/proc/bus/usb/devices"   2&nbsp;USB1="Spd=12"   3&nbsp;USB2="Spd=480"   4&nbsp;   5&nbsp;   6&nbsp;bus_speed=$(grep Spd $devfile | awk '{print $9}')   7&nbsp;   8&nbsp;if [ "$bus_speed" = "$USB1" ]   9&nbsp;then  10&nbsp;  echo "USB 1.1 port found."  11&nbsp;  # Do something appropriate for USB 1.1.  12&nbsp;fi</PRE></TD></TR></TABLE></P><P>The <TTCLASS="FILENAME">/proc</TT> directory	   contains subdirectories with unusual numerical	   names.  Every one of these names maps to the <AHREF="variables2.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 <TTCLASS="FILENAME">stat</TT> and	   <TTCLASS="FILENAME">status</TT> files keep running statistics	   on the process, the <TTCLASS="FILENAME">cmdline</TT> file holds	   the command-line arguments the process was invoked with, and	   the <TTCLASS="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><DIVCLASS="EXAMPLE"><HR><ANAME="PIDID"></A><P><B>Example 27-2. Finding the process associated with a PID</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# pid-identifier.sh: Gives complete path name to process associated with pid.   3&nbsp;   4&nbsp;ARGNO=1  # Number of arguments the script expects.   5&nbsp;E_WRONGARGS=65   6&nbsp;E_BADPID=66   7&nbsp;E_NOSUCHPROCESS=67   8&nbsp;E_NOPERMISSION=68   9&nbsp;PROCFILE=exe  10&nbsp;  11&nbsp;if [ $# -ne $ARGNO ]  12&nbsp;then  13&nbsp;  echo "Usage: `basename $0` PID-number" &#62;&#38;2  # Error message &#62;stderr.  14&nbsp;  exit $E_WRONGARGS  15&nbsp;fi    16&nbsp;  17&nbsp;pidno=$( ps ax | grep $1 | awk '{ print $1 }' | grep $1 )  18&nbsp;# Checks for pid in "ps" listing, field #1.  19&nbsp;# Then makes sure it is the actual process, not the process invoked by this script.  20&nbsp;# The last "grep $1" filters out this possibility.  21&nbsp;#  22&nbsp;#    pidno=$( ps ax | awk '{ print $1 }' | grep $1 )  23&nbsp;#    also works, as Teemu Huovila, points out.  24&nbsp;  25&nbsp;if [ -z "$pidno" ]  # If, after all the filtering, the result is a zero-length string,  26&nbsp;then                # no running process corresponds to the pid given.  27&nbsp;  echo "No such process running."  28&nbsp;  exit $E_NOSUCHPROCESS  29&nbsp;fi    30&nbsp;  31&nbsp;# Alternatively:  32&nbsp;#   if ! ps $1 &#62; /dev/null 2&#62;&#38;1  33&nbsp;#   then                # no running process corresponds to the pid given.  34&nbsp;#     echo "No such process running."  35&nbsp;#     exit $E_NOSUCHPROCESS  36&nbsp;#    fi  37&nbsp;  38&nbsp;# To simplify the entire process, use "pidof".  39&nbsp;  40&nbsp;  41&nbsp;if [ ! -r "/proc/$1/$PROCFILE" ]  # Check for read permission.  42&nbsp;then  43&nbsp;  echo "Process $1 running, but..."  44&nbsp;  echo "Can't get read permission on /proc/$1/$PROCFILE."  45&nbsp;  exit $E_NOPERMISSION  # Ordinary user can't access some files in /proc.  46&nbsp;fi    47&nbsp;  48&nbsp;# The last two tests may be replaced by:  49&nbsp;#    if ! kill -0 $1 &#62; /dev/null 2&#62;&#38;1 # '0' is not a signal, but  50&nbsp;                                      # this will test whether it is possible  51&nbsp;                                      # to send a signal to the process.  52&nbsp;#    then echo "PID doesn't exist or you're not its owner" &#62;&#38;2  53&nbsp;#    exit $E_BADPID  54&nbsp;#    fi  55&nbsp;  56&nbsp;  57&nbsp;  58&nbsp;exe_file=$( ls -l /proc/$1 | grep "exe" | awk '{ print $11 }' )  59&nbsp;# Or       exe_file=$( ls -l /proc/$1/exe | awk '{print $11}' )  60&nbsp;#  61&nbsp;# /proc/pid-number/exe is a symbolic link  62&nbsp;# to the complete path name of the invoking process.  63&nbsp;  64&nbsp;if [ -e "$exe_file" ]  # If /proc/pid-number/exe exists...  65&nbsp;then                 # the corresponding process exists.  66&nbsp;  echo "Process #$1 invoked by $exe_file."  67&nbsp;else  68&nbsp;  echo "No such process running."  69&nbsp;fi    70&nbsp;  71&nbsp;  72&nbsp;# This elaborate script can *almost* be replaced by  73&nbsp;# ps ax | grep $1 | awk '{ print $5 }'  74&nbsp;# However, this will not work...  75&nbsp;# because the fifth field of 'ps' is argv[0] of the process,  76&nbsp;# not the executable file path.  77&nbsp;#  78&nbsp;# However, either of the following would work.  79&nbsp;#       find /proc/$1/exe -printf '%l\n'  80&nbsp;#       lsof -aFn -p $1 -d txt | sed -ne 's/^n//p'  81&nbsp;  82&nbsp;# Additional commentary by Stephane Chazelas.  83&nbsp;  84&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="CONSTAT"></A><P><B>Example 27-3. On-line connect status</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;PROCNAME=pppd        # ppp daemon   4&nbsp;PROCFILENAME=status  # Where to look.   5&nbsp;NOTCONNECTED=65   6&nbsp;INTERVAL=2           # Update every 2 seconds.   7&nbsp;   8&nbsp;pidno=$( ps ax | grep -v "ps ax" | grep -v grep | grep $PROCNAME | awk '{ print $1 }' )   9&nbsp;# Finding the process number of 'pppd', the 'ppp daemon'.  10&nbsp;# Have to filter out the process lines generated by the search itself.  11&nbsp;#  12&nbsp;#  However, as Oleg Philon points out,  13&nbsp;#+ this could have been considerably simplified by using "pidof".  14&nbsp;#  pidno=$( pidof $PROCNAME )  15&nbsp;#  16&nbsp;#  Moral of the story:  17&nbsp;#+ When a command sequence gets too complex, look for a shortcut.  18&nbsp;  19&nbsp;  20&nbsp;if [ -z "$pidno" ]   # If no pid, then process is not running.  21&nbsp;then  22&nbsp;  echo "Not connected."  23&nbsp;  exit $NOTCONNECTED  24&nbsp;else  25&nbsp;  echo "Connected."; echo  26&nbsp;fi  27&nbsp;  28&nbsp;while [ true ]       # Endless loop, script can be improved here.  29&nbsp;do  30&nbsp;  31&nbsp;  if [ ! -e "/proc/$pidno/$PROCFILENAME" ]  32&nbsp;  # While process running, then "status" file exists.  33&nbsp;  then  34&nbsp;    echo "Disconnected."  35&nbsp;    exit $NOTCONNECTED  36&nbsp;  fi  37&nbsp;  38&nbsp;netstat -s | grep "packets received"  # Get some connect statistics.  39&nbsp;netstat -s | grep "packets delivered"  40&nbsp;  41&nbsp;  42&nbsp;  sleep $INTERVAL  43&nbsp;  echo; echo  44&nbsp;  45&nbsp;done  46&nbsp;  47&nbsp;exit 0  48&nbsp;  49&nbsp;# As it stands, this script must be terminated with a Control-C.  50&nbsp;  51&nbsp;#    Exercises:  52&nbsp;#    ---------  53&nbsp;#    Improve the script so it exits on a "q" keystroke.  54&nbsp;#    Make the script more user-friendly in other ways.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="WARNING"><TABLECLASS="WARNING"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/warning.png"HSPACE="5"ALT="Warning"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>In general, it is dangerous to	 <ICLASS="EMPHASIS">write</I> to the files in <TTCLASS="FILENAME">/proc</TT>, as this can corrupt the	 filesystem or crash the machine.</P></TD></TR></TABLE></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN14673"HREF="procref1.html#AEN14673">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Certain system commands, such as	     <AHREF="system.html#PROCINFOREF">procinfo</A>,	     <AHREF="system.html#FREEREF">free</A>,	     <AHREF="system.html#VMSTATREF">vmstat</A>,	     <AHREF="system.html#LSDEVREF">lsdev</A>,	     and <AHREF="system.html#UPTIMEREF">uptime</A>	     do this as well.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="devproc.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="zeros.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">/dev and /proc</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="devproc.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Of Zeros and Nulls</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -