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

📄 system.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
   6&nbsp;# ...   7&nbsp;# Jul  7 20:48:58 localhost ./test.sh[1712]: Logging at line 3.</PRE></TD></TR></TABLE>            </P></DD><DT><BCLASS="COMMAND">logrotate</B></DT><DD><P>This utility manages the system log files, rotating,	      compressing, deleting, and/or e-mailing them, as appropriate.	      This keeps the <TTCLASS="FILENAME">/var/log</TT>	      from getting cluttered with old log files.	      Usually <AHREF="system.html#CRONREF">cron</A> runs	      <BCLASS="COMMAND">logrotate</B> on a daily basis.</P><P>Adding an appropriate entry to	       <TTCLASS="FILENAME">/etc/logrotate.conf</TT> makes it possible	       to manage personal log files, as well as system-wide	       ones.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Stefano Falsetto has created <AHREF="http://www.gnu.org/software/rottlog/"TARGET="_top">rottlog</A>,	     which he considers to be an improved version of	     <BCLASS="COMMAND">logrotate</B>.</P></TD></TR></TABLE></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="JOBCONTROLSYS1"></A>Job Control</B></P><DL><DT><BCLASS="COMMAND">ps</B></DT><DD><P><TTCLASS="REPLACEABLE"><I>P</I></TT>rocess	      <TTCLASS="REPLACEABLE"><I>S</I></TT>tatistics: lists currently	      executing processes by owner and PID (process ID). This	      is usually invoked with <TTCLASS="OPTION">ax</TT> options,	      and may be piped to <AHREF="textproc.html#GREPREF">grep</A>	      or <AHREF="sedawk.html#SEDREF">sed</A> to search for a	      specific process (see <AHREF="internal.html#EX44">Example 11-12</A> and <AHREF="procref1.html#PIDID">Example 27-2</A>).</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B> ps ax | grep sendmail</B></TT> <TTCLASS="COMPUTEROUTPUT">295 ?	   S	  0:00 sendmail: accepting connections on port 25</TT></PRE></TD></TR></TABLE><P>To display system processes in graphical <SPANCLASS="QUOTE">"tree"</SPAN>	      format: <BCLASS="COMMAND">ps afjx</B> or	      <BCLASS="COMMAND">ps ax --forest</B>.</P></DD><DT><BCLASS="COMMAND">pgrep</B>, <BCLASS="COMMAND">pkill</B></DT><DD><P>Combining the <BCLASS="COMMAND">ps</B> command	      with <AHREF="textproc.html#GREPREF">grep</A> or	      <AHREF="internal.html#KILLREF">kill</A>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ps a | grep mingetty</B></TT> <TTCLASS="COMPUTEROUTPUT">2212 tty2     Ss+    0:00 /sbin/mingetty tty2 2213 tty3     Ss+    0:00 /sbin/mingetty tty3 2214 tty4     Ss+    0:00 /sbin/mingetty tty4 2215 tty5     Ss+    0:00 /sbin/mingetty tty5 2216 tty6     Ss+    0:00 /sbin/mingetty tty6 4849 pts/2    S+     0:00 grep mingetty</TT>   <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>pgrep mingetty</B></TT> <TTCLASS="COMPUTEROUTPUT">2212 mingetty 2213 mingetty 2214 mingetty 2215 mingetty 2216 mingetty</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><BCLASS="COMMAND">pstree</B></DT><DD><P>Lists currently executing processes in	      <SPANCLASS="QUOTE">"tree"</SPAN> format. The <TTCLASS="OPTION">-p</TT> option	      shows the PIDs, as well as the process names.</P></DD><DT><BCLASS="COMMAND">top</B></DT><DD><P>Continuously updated display of most cpu-intensive	      processes. The <TTCLASS="OPTION">-b</TT> option displays in text	      mode, so that the output may be parsed or accessed from	      a script.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>top -b</B></TT> <TTCLASS="COMPUTEROUTPUT">  8:30pm  up 3 min,  3 users,  load average: 0.49, 0.32, 0.13 45 processes: 44 sleeping, 1 running, 0 zombie, 0 stopped CPU states: 13.6% user,  7.3% system,  0.0% nice, 78.9% idle Mem:    78396K av,   65468K used,   12928K free,       0K shrd,    2352K buff Swap:  157208K av,       0K used,  157208K free                   37244K cached   PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND   848 bozo      17   0   996  996   800 R     5.6  1.2   0:00 top     1 root       8   0   512  512   444 S     0.0  0.6   0:04 init     2 root       9   0     0    0     0 SW    0.0  0.0   0:00 keventd   ...</TT>   	      </PRE></TD></TR></TABLE>	    </P></DD><DT><BCLASS="COMMAND">nice</B></DT><DD><P>Run a background job with an altered	      priority. Priorities run from 19 (lowest) to -20	      (highest). Only <ICLASS="EMPHASIS">root</I> may set the	      negative (higher) priorities. Related commands are	      <BCLASS="COMMAND">renice</B>, <BCLASS="COMMAND">snice</B>,	      and <BCLASS="COMMAND">skill</B>.</P></DD><DT><BCLASS="COMMAND">nohup</B></DT><DD><P>Keeps a command running even after user logs off.	      The command will run as a foreground process unless followed	      by <SPANCLASS="TOKEN">&#38;</SPAN>.  If you use <BCLASS="COMMAND">nohup</B>	      within a script, consider coupling it with a <AHREF="internal.html#WAITREF">wait</A> to avoid creating an orphan	      or zombie process.</P></DD><DT><ANAME="PIDOFREF"></A><BCLASS="COMMAND">pidof</B></DT><DD><P>Identifies <ICLASS="EMPHASIS">process ID (PID)</I> of a	    running job. Since job control commands, such as <AHREF="internal.html#KILLREF">kill</A> and <BCLASS="COMMAND">renice</B>	    act on the <ICLASS="EMPHASIS">PID</I> of a process (not	    its name), it is sometimes necessary to identify that	    <ICLASS="EMPHASIS">PID</I>.  The <BCLASS="COMMAND">pidof</B>	    command is the approximate counterpart to the <AHREF="variables2.html#PPIDREF">$PPID</A> internal variable.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>pidof xclock</B></TT> <TTCLASS="COMPUTEROUTPUT">880</TT> 	      </PRE></TD></TR></TABLE>	    </P><DIVCLASS="EXAMPLE"><HR><ANAME="KILLPROCESS"></A><P><B>Example 13-6. <BCLASS="COMMAND">pidof</B> helps kill a process</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# kill-process.sh   3&nbsp;   4&nbsp;NOPROCESS=2   5&nbsp;   6&nbsp;process=xxxyyyzzz  # Use nonexistent process.   7&nbsp;# For demo purposes only...   8&nbsp;# ... don't want to actually kill any actual process with this script.   9&nbsp;#  10&nbsp;# If, for example, you wanted to use this script to logoff the Internet,  11&nbsp;#     process=pppd  12&nbsp;  13&nbsp;t=`pidof $process`       # Find pid (process id) of $process.  14&nbsp;# The pid is needed by 'kill' (can't 'kill' by program name).  15&nbsp;  16&nbsp;if [ -z "$t" ]           # If process not present, 'pidof' returns null.  17&nbsp;then  18&nbsp;  echo "Process $process was not running."  19&nbsp;  echo "Nothing killed."  20&nbsp;  exit $NOPROCESS  21&nbsp;fi    22&nbsp;  23&nbsp;kill $t                  # May need 'kill -9' for stubborn process.  24&nbsp;  25&nbsp;# Need a check here to see if process allowed itself to be killed.  26&nbsp;# Perhaps another " t=`pidof $process` " or ...  27&nbsp;  28&nbsp;  29&nbsp;# This entire script could be replaced by  30&nbsp;#    kill $(pidof -x process_name)  31&nbsp;# but it would not be as instructive.  32&nbsp;  33&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><BCLASS="COMMAND">fuser</B></DT><DD><P>Identifies the processes (by PID) that are accessing	      a given file, set of files, or directory. May also be	      invoked with the <TTCLASS="OPTION">-k</TT> option, which kills	      those processes. This has interesting implications for	      system security, especially in scripts preventing	      unauthorized users from accessing system services.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>fuser -u /usr/bin/vim</B></TT> <TTCLASS="COMPUTEROUTPUT">/usr/bin/vim:         3207e(bozo)</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>fuser -u /dev/null</B></TT> <TTCLASS="COMPUTEROUTPUT">/dev/null:            3009(bozo)  3010(bozo)  3197(bozo)  3199(bozo)</TT> 	      </PRE></TD></TR></TABLE>	    </P><P>One important application for <BCLASS="COMMAND">fuser</B> is	     when physically inserting or removing storage media, such	     as CD ROM disks or USB flash drives. Sometimes trying	     a <AHREF="system.html#UMOUNTREF">umount</A> fails with a	     <SPANCLASS="ERRORNAME">device is busy</SPAN> error message. This	     means that some user(s) and/or process(es) are accessing	     the device. An <BCLASS="COMMAND">fuser -um /dev/device_name</B>	     will clear up the mystery, so you can kill any relevant	     processes.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>umount /mnt/usbdrive</B></TT> <TTCLASS="COMPUTEROUTPUT">umount: /mnt/usbdrive: device is busy</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>fuser -um /dev/usbdrive</B></TT> <TTCLASS="COMPUTEROUTPUT">/mnt/usbdrive:        1772c(bozo)</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>kill -9 1772</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>umount /mnt/usbdrive</B></TT> 	      </PRE></TD></TR></TABLE>	    </P><P> The <BCLASS="COMMAND">fuser</B> command, invoked with the	      <TTCLASS="OPTION">-n</TT> option identifies the processes	      accessing a <ICLASS="EMPHASIS">port</I>. This	      is especially useful in combination with <AHREF="system.html#NMAPREF">nmap</A>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">root# </TT><TTCLASS="USERINPUT"><B>nmap localhost.localdomain</B></TT> <TTCLASS="COMPUTEROUTPUT">PORT     STATE SERVICE 25/tcp   open  smtp</TT>    <TTCLASS="PROMPT">root# </TT><TTCLASS="USERINPUT"><B>fuser -un tcp 25</B></TT> <TTCLASS="COMPUTEROUTPUT">25/tcp:               2095(root)</TT>  <TTCLASS="PROMPT">root# </TT><TTCLASS="USERINPUT"><B>ps ax | grep 2095 | grep -v grep</B></TT> <TTCLASS="COMPUTEROUTPUT">2095 ?        Ss     0:00 sendmail: accepting connections</TT> 	      </PRE></TD></TR></TABLE>            </P></DD><DT><ANAME="CRONREF"></A><BCLASS="COMMAND">cron</B></DT><DD><P>Administrative program scheduler, performing such	      duties as cleaning up and deleting system log	      files and updating the <SPANCLASS="DATABASE">slocate</SPAN>	      database. This is the superuser version of <AHREF="timedate.html#ATREF">at</A> (although each user may have their	      own <TTCLASS="FILENAME">crontab</TT> file which can be changed	      with the <BCLASS="COMMAND">crontab</B> command).  It runs	      as a <AHREF="communications.html#DAEMONREF">daemon</A> and executes	      scheduled entries from <TTCLASS="FILENAME">/etc/crontab</TT>.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Some flavors of Linux run	      <BCLASS="COMMAND">crond</B>, Matthew Dillon's version of	      <BCLASS="COMMAND">cron</B>.</P></TD></TR></TABLE></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="RUNCONTROLSYS1"></A>Process Control and Booting</B></P><DL><DT><BCLASS="COMMAND">init</B></DT><DD><P><ANAME="INITTABREF"></A></P><P>The <BCLASS="COMMAND">init</B> command is the <AHREF="internal.html#FORKREF">parent</A> of all processes. Called	      in the final step of a bootup, <BCLASS="COMMAND">init</B>	      determines the runlevel of the system from	      <TTCLASS="FILENAME">/etc/inittab</TT>. Invoked by its alias	      <BCLASS="COMMAND">telinit</B>, and by root only.</P></DD><DT><BCLASS="COMMAND">telinit</B></DT><DD><P>Symlinked to <BCLASS="COMMAND">init</B>, this is a means of changing the system runlevel,	      usually done for system maintenance or emergency filesystem	      rep

⌨️ 快捷键说明

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