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

📄 lsg20.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<FONT COLOR="#000080">$ ps -a







 PID TTY STAT TIME COMMAND







 1 psf S 0:00 init







 6 psf S 0:00 update (sync)







 23 psf S 0:00 /usr/sbin/crond -l10







 29 psf S 0:00 /usr/sbin/syslogd







 31 psf S 0:00 /usr/sbin/klogd







 33 psf S 0:00 /usr/sbin/lpd







 40 psf S 0:00 selection -t ms







 42 v02 S 0:01 -bash







 43 v03 S 0:00 /sbin/agetty 38400 tty3







 44 v04 S 0:00 /sbin/agetty 38400 tty4







 45 v05 S 0:00 /sbin/agetty 38400 tty5







 46 v06 S 0:00 /sbin/agetty 38400 tty6







 41 v01 S 0:01 -bash







 140 v01 R 0:00 ps -a</FONT></PRE>







<P>This relatively short output shows a very lightly loaded system. Most of the entries are the Linux operating system kernel and daemons, as well as serial port getty processes. Only the last two commands are started by the user who issued the ps command. Of course, you can't tell who started each process with this output. To see who started each process, you can combine the -u and -a options (note that you use only one hyphen, followed by the option letters):







<BR>







<PRE>







<FONT COLOR="#000080">$ ps -au







USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND







root 1 0.0 3.0 44 208 psf S 23:19 0:00 init







root 6 0.0 1.8 24 128 psf S 23:19 0:00 update (sync)







root 23 0.0 3.0 56 212 psf S 23:19 0:00 /usr/sbin/crond -l10







root 29 0.0 3.4 61 236 psf S 23:19 0:00 /usr/sbin/syslogd







root 31 0.0 2.8 36 200 psf S 23:19 0:00 /usr/sbin/klogd







root 33 0.0 2.9 64 204 psf S 23:19 0:00 /usr/sbin/lpd







root 40 0.0 2.0 32 140 psf S 23:19 0:00 selection -t ms







root 42 0.1 6.9 372 480 v02 S 23:19 0:01 -bash







root 43 0.0 2.3 37 164 v03 S 23:19 0:00 /sbin/agetty 38400 tt







root 44 0.0 2.3 37 164 v04 S 23:19 0:00 /sbin/agetty 38400 tt







root 45 0.0 2.3 37 164 v05 S 23:19 0:00 /sbin/agetty 38400 tt







root 46 0.0 2.3 37 164 v06 S 23:19 0:00 /sbin/agetty 38400 tt







yvonne 41 0.0 6.8 364 472 v01 S 23:19 0:01 -bash







yvonne 2519 0.0 3.4 80 236 v01 R 23:39 0:00 ps -ua</FONT></PRE>







<P>This command produces a list with all the same columns as the -u option, but it shows all the processes running on the system. The order in which you enter the options doesn't matter, so -au is functionally the same as -ua.







<BR>







<P>A few other ps command line options are occasionally useful. The -l option adds information about which processes started each process (useful when you want to identify child processes):







<BR>







<PRE>







<FONT COLOR="#000080">$ ps -l







 F UID PID PPID PRI NI SIZE RSS WCHAN STAT TTY TIME COMMAND







 0 501 41 1 15 0 364 472 114d9c S v01 0:00 -bash







 0 501 121 41 29 0 64 208 0 R v01 0:00 ps -l</FONT></PRE>







<P>The PPID (Parent Process ID) column shows which process started that particular process. The preceding extract shows that the ps command was started by the bash process, as the shell is the parent of all user commands. The PPID for the login Bourne shell is PID 1, which is the init process of the operating system. (Think about what this relationship means. If init ever terminates, all other processes die, too.)







<BR>







<BLOCKQUOTE>







<BLOCKQUOTE>







<HR ALIGN=CENTER>







<BR>







<NOTE>The Linux version of the ps command has a few idiosyncrasies. The hyphen before an option is not strictly necessary, so ps u works as well as ps -u. However, because UNIX convention (and most UNIX versions) require a hyphen, you should use them.</NOTE>







<BR>







<HR ALIGN=CENTER>







</BLOCKQUOTE></BLOCKQUOTE>







<P>Most system administrators get by with three versions of the ps command (when logged in as root). To display information about the system as a whole, the following two command lines show practically everything there is to know about processes:







<BR>







<PRE>







<FONT COLOR="#000080">ps -ef







ps -le</FONT></PRE>







<P>The meaning of the primary columns in the output from the two commands has been mentioned earlier in this section. The rest of the columns are either evident from their shortform or are not that important. For complete information, see the ps man page (which is not entirely accurate or complete, unfortunately).







<BR>







<BR>







<A NAME="E68E110"></A>







<H3 ALIGN=CENTER>







<CENTER>







<FONT SIZE=5 COLOR="#FF0000"><B>Using kill</B></FONT></CENTER></H3>







<BR>







<P>A process that locks up a terminal or doesn't do anything is generally referred to as a hung process. Sometimes a user has a process that doesn't terminate properly (especially common with programmers). This kind of process is called a runaway process. In both cases, the only way to get rid of the process and restore some normalcy to the system is to terminate the process by issuing the kill command.







<BR>







<P>To use kill, you must have access to another window or console where you can issue commands. If your terminal is completely locked up, you will have to find another one from which to log in. As a user, you can only kill your own processes; you cannot affect any process another user or the system is running. As root, you can terminate any process with the kill command.







<BR>







<P>In order to use the kill command, you need the process identification number (PID) of the process to be terminated. Use the ps command, as explained in the preceding section, to find out this information. Next, use the kill command with the PID as an argument. For example, the following terminal session shows a user process called bad_prog started by walter that has hung up and needs to be killed. The PID is obtained by displaying all of walter's processes:







<BR>







<PRE>







<FONT COLOR="#000080">$ ps -u walter







USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND







walter 561 0.1 6.8 364 472 v01 S 13:19 0:01 -bash







walter 598 9.3 4.1 2736 472 v01 R 15:26 2:01 bad_prog







$ kill 598</FONT></PRE>







<P>When you issue the kill command, you don't get any return message if it works properly. The only way to verify that the process was properly terminated is to issue another ps command and look for the PID or process name.







<BR>







<P>Because some processes spawn child processes with different PIDs, you must be sure to check that all the child processes are terminated as well. The best way to do this is to watch the names of the executing processes for a few minutes to ensure that the child isn't dormant, only to return later. This problem usually happens while the child processes are being generated by a parent. Check the PPID column (use the ps -l option) to see which process is the parent and terminate that process as well.







<BR>







<BLOCKQUOTE>







<BLOCKQUOTE>







<HR ALIGN=CENTER>







<BR>







<NOTE>When you are killing processes and are logged in as root, make sure you type the correct PID or you may inadvertently terminate another process. Check the PID carefully! Also, don't kill any system processes unless you know what they do and why they need to be terminated.</NOTE>







<BR>







<HR ALIGN=CENTER>







</BLOCKQUOTE></BLOCKQUOTE>







<P>If the process doesn't terminate properly with the kill command, you need to use sterner measures. The kill command has several levels of operation. When issued with no arguments other than the PID, the kill command tries to gracefully terminate the process (which means any open files are closed and kill is generally polite to the process). If this command doesn't work, use the -9 option, which is a little more forceful in its attempt to terminate the process. Essentially, the command tries to terminate the process without regard to open files or child processes, although you seldom have to worry about problems with this type of termination because Linux handles it all. For example, to forcefully terminate the process with PID 726, issue the following command:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">kill -9 726</FONT></PRE>







<P>If the process still doesn't terminate, it's time to get ruthless and use the -15 option, the most potent form of kill command. Only use this option when the other forms of the kill command are not working, as it doesn't try to be nice to the process or any open files at all. To use this option on the same sample process, issue the command:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">kill -15 726</FONT></PRE>







<P>If that doesn't work, the process may be unkillable. This situation does happen quite often with Linux, and the only solution is to shut down and reboot the machine.







<BR>







<P>To help prevent a user from killing other user's processes, ps checks for the process owner when you issue a kill command. If a user tries to kill another user's process, a message like the following one is displayed:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">kill: - Not owner</FONT></PRE>







<P>The superuser doesn't get this message because the superuser login can kill anything except some system processes (such as init).







<BR>







<BR>







<A NAME="E68E111"></A>







<H3 ALIGN=CENTER>







<CENTER>







<FONT SIZE=5 COLOR="#FF0000"><B>Using the top Command</B></FONT></CENTER></H3>







<BR>







<P>Sometimes you may want to watch the system's behavior to spot problems, monitor system loading, or check for runaway processes. Instead of running the ps command at regular intervals, Linux offers the top command as an alternative. When you issue the top command, the screen shows a continual snapshot of the system, taken every five seconds (unless you specify a different time increment). By default, top shows the most CPU-intensive tasks on the system as a full-screen display.







<BR>







<P>The syntax of the top command allows you to alter much of the utility's behavior from the command line, although most changes are also available from within top:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">top [-] [d delay] [q] [S] [s] [i]</FONT></PRE>







<P>The command line options supported by top are as follows:







<BR>















<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







d







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Specifies the delay between screen updates (can be changed from within top using the s command)</FONT>







⌨️ 快捷键说明

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