跟散仙學shell命令(三)

本篇散仙主要講一些,linux進程的管理,磁盤信息統計,CPU,內存等命令,非常實用。 

實際開發中,linux系統的情況,能直接反映服務器運行的信息,cpu負載,內存佔用,磁盤等等,所以掌握這些命令,有助於我們更快更專業管理linux系統。 


(1),ps命令,探查系統進程。 

Java代碼  收藏代碼

  1. [search@h1 ~]$ ps  

  2.   PID TTY          TIME CMD  

  3. 17847 pts/0    00:00:00 bash  

  4. 17977 pts/0    00:00:00 ps  

  5. [search@h1 ~]$   



默認情況下,ps命令不會顯示太多的信息,只顯示了pid進程號,和它運行在那個終端TTY,以及進程已用的CPU時間,和它執行的命令。 

那麼,通常在實際開發中,通常會用ps -efl 命令,裏顯示更多的信息。 

Java代碼  收藏代碼

  1. F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD  

  2. 4 S root         1     0  0  80   0 -  4808 poll_s Aug01 ?        00:00:01 /sbin/init  

  3. 1 S root         2     0  0  80   0 -     0 kthrea Aug01 ?        00:00:00 [kthreadd]  

  4. 1 S root         3     2  0 -40   - -     0 migrat Aug01 ?        00:00:00 [migration/0]  

  5. 1 S root         4     2  0  80   0 -     0 ksofti Aug01 ?        00:00:00 [ksoftirqd/0]  

  6. 1 S root         5     2  0 -40   - -     0 cpu_st Aug01 ?        00:00:00 [migration/0]  

  7. 5 S root         6     2  0 -40   - -     0 watchd Aug01 ?        00:00:00 [watchdog/0]  

  8. 1 S root         7     2  0  80   0 -     0 worker Aug01 ?        00:00:15 [events/0]  

  9. 1 S root         8     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [cgroup]  

  10. 1 S root         9     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [khelper]  

  11. 1 S root        10     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [netns]  

  12. 1 S root        11     2  0  80   0 -     0 async_ Aug01 ?        00:00:00 [async/mgr]  

  13. 1 S root        12     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [pm]  

  14. 1 S root        13     2  0  80   0 -     0 bdi_sy Aug01 ?        00:00:00 [sync_supers]  

  15. 1 S root        14     2  0  80   0 -     0 bdi_fo Aug01 ?        00:00:00 [bdi-default]  

  16. 1 S root        15     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kintegrityd/0]  

  17. 1 S root        16     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kblockd/0]  

  18. 1 S root        17     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kacpid]  

  19. 1 S root        18     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kacpi_notify]  

  20. 1 S root        19     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kacpi_hotplug]  

  21. 1 S root        20     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [ata_aux]  

  22. 1 S root        21     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [ata_sff/0]  

  23. 1 S root        22     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [ksuspend_usbd]  

  24. 5 S root        23     2  0  80   0 -     0 hub_th Aug01 ?        00:00:00 [khubd]  

  25. 5 S root        24     2  0  80   0 -     0 serio_ Aug01 ?        00:00:00 [kseriod]  

  26. 1 S root        25     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [md/0]  

  27. 1 S root        26     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [md_misc/0]  

  28. 1 S root        27     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [linkwatch]  

  29. 1 S root        28     2  0  80   0 -     0 watchd Aug01 ?        00:00:00 [khungtaskd]  

  30. 1 S root        29     2  0  80   0 -     0 kswapd Aug01 ?        00:00:00 [kswapd0]  

  31. 1 S root        30     2  0  85   5 -     0 ksm_sc Aug01 ?        00:00:00 [ksmd]  

  32. 1 S root        31     2  0  99  19 -     0 khugep Aug01 ?        00:00:00 [khugepaged]  

  33. 1 S root        32     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [aio/0]  

  34. 1 S root        33     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [crypto/0]  

  35. 1 S root        38     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kthrotld/0]  

  36. 1 S root        39     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [pciehpd]  

  37. 1 S root        41     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kpsmoused]  

  38. 1 S root        42     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [usbhid_resumer]  

  39. 1 S root        73     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kstriped]  

  40. 1 S root       145     2  0  80   0 -     0 scsi_e Aug01 ?        00:00:00 [scsi_eh_0]  

  41. 1 S root       146     2  0  80   0 -     0 scsi_e Aug01 ?        00:00:00 [scsi_eh_1]  

  42. 1 S root       153     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [mpt_poll_0]  

  43. 1 S root       154     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [mpt/0]  

  44. 1 S root       155     2  0  80   0 -     0 scsi_e Aug01 ?        00:00:00 [scsi_eh_2]  

  45. 1 S root       295     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kdmflush]  

  46. 1 S root       297     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [kdmflush]  

  47. 1 S root       314     2  0  80   0 -     0 kjourn Aug01 ?        00:00:00 [jbd2/dm-0-8]  

  48. 1 S root       315     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [ext4-dio-unwrit]  

  49. 5 S root       389     1  0  76  -4 -  2769 poll_s Aug01 ?        00:00:00 /sbin/udevd -d  

  50. 1 S root       408     2  0  80   0 -     0 bdi_wr Aug01 ?        00:00:00 [flush-253:0]  

  51. 1 S root       602     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [vmmemctl]  

  52. 1 S root       721     2  0  80   0 -     0 kjourn Aug01 ?        00:00:00 [jbd2/sda1-8]  

  53. 1 S root       722     2  0  80   0 -     0 worker Aug01 ?        00:00:00 [ext4-dio-unwrit]  

  54. 1 S root       765     2  0  80   0 -     0 kaudit Aug01 ?        00:00:00 [kauditd]  

  55. 1 S root       904     1  0  80   0 -  2280 poll_s Aug01 ?        00:00:00 /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0  

  56. 5 S root       941     1  0  80   0 - 62799 poll_s Aug01 ?        00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5  

  57. 5 S root       983     1  0  80   0 - 16651 poll_s Aug01 ?        00:00:00 /usr/sbin/sshd  

  58. 4 S root      1077     1  0  80   0 - 20318 ep_pol Aug01 ?        00:00:00 /usr/libexec/postfix/master  

  59. 1 S root      1091     1  0  80   0 - 29323 hrtime Aug01 ?        00:00:00 crond  

  60. 4 S postfix   1102  1077  0  80   0 - 20380 ep_pol Aug01 ?        00:00:00 qmgr -l -t fifo -u  

  61. 4 S root      1109     1  0  80   0 -  1016 n_tty_ Aug01 tty1     00:00:00 /sbin/mingetty /dev/tty1  

  62. 4 S root      1111     1  0  80   0 -  1016 n_tty_ Aug01 tty2     00:00:00 /sbin/mingetty /dev/tty2  

  63. 4 S root      1113     1  0  80   0 -  1016 n_tty_ Aug01 tty3     00:00:00 /sbin/mingetty /dev/tty3  

  64. 4 S root      1115     1  0  80   0 -  1016 n_tty_ Aug01 tty4     00:00:00 /sbin/mingetty /dev/tty4  

  65. 4 S root      1118     1  0  80   0 -  1016 n_tty_ Aug01 tty5     00:00:00 /sbin/mingetty /dev/tty5  

  66. 4 S root      1122     1  0  80   0 -  1016 n_tty_ Aug01 tty6     00:00:00 /sbin/mingetty /dev/tty6  

  67. 5 S root      1127   389  0  78  -2 -  2768 poll_s Aug01 ?        00:00:00 /sbin/udevd -d  

  68. 5 S root      1128   389  0  78  -2 -  2768 poll_s Aug01 ?        00:00:00 /sbin/udevd -d  

  69. 4 S root     17790   983  0  80   0 - 25099 poll_s 04:01 ?        00:00:00 sshd: root@pts/0   

  70. 4 S root     17795 17790  0  80   0 - 27104 wait   04:01 pts/0    00:00:00 -bash  

  71. 4 S root     17846 17795  0  80   0 - 36358 wait   04:14 pts/0    00:00:00 su - search  

  72. 4 S search   17847 17846  0  80   0 - 27107 wait   04:14 pts/0    00:00:00 -bash  

  73. 4 S postfix  17972  1077  0  80   0 - 20338 ep_pol 04:29 ?        00:00:00 pickup -l -t fifo -u  

  74. 0 R search   17979 17847  0  80   0 - 27558 -      04:44 pts/0    00:00:00 ps -efl  

  75. [search@h1 ~]$   



-e參數主要顯示所有的進程, 
-f參數主要顯示完整的格式輸出 
-l參數,出先額外的進程標記,優先級等等 

下面介紹一下,這幾列分別代表的意思 
UID,代表啓動這些進程的用戶 
PID,進程的進程號 
PPID,父進程的進程號,代表該進程,是由它的父進程啓動的 
C,j進程生命週期中的CPU利用率 
STIME,進程啓動的系統時間 
TTY,進程啓動時的終端設備 
TIME,運行進程需要的累計的CPU的時間 
CMD,啓動的程序名稱 
F,內核分配給進程的系統標記 
S,進程的狀態,0代表運行,S代表正在休眠,R代表可運行,等待運行,Z代表殭屍進程 
PRI,進程的優先級,越大的數字優先級越低 
NI,謙讓度值,用來參與優先級 
ADDR,進程的內存地址 
SZ,假如進程被喚出,需要的交換空間的大小 
WCHAN,進程休眠的內核函數的地址 

ps -efH參數,可以以樹狀的方式,顯示進程信息,我們可以很輕鬆的看到那些進程啓動了那些進程。 

Java代碼  收藏代碼

  1. root       721     2  0 Aug01 ?        00:00:00   [jbd2/sda1-8]  

  2. root       722     2  0 Aug01 ?        00:00:00   [ext4-dio-unwrit]  

  3. root       765     2  0 Aug01 ?        00:00:00   [kauditd]  

  4. root         1     0  0 Aug01 ?        00:00:01 /sbin/init  

  5. root       389     1  0 Aug01 ?        00:00:00   /sbin/udevd -d  

  6. root      1127   389  0 Aug01 ?        00:00:00     /sbin/udevd -d  

  7. root      1128   389  0 Aug01 ?        00:00:00     /sbin/udevd -d  

  8. root       904     1  0 Aug01 ?        00:00:00   /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0  

  9. root       941     1  0 Aug01 ?        00:00:00   /sbin/rsyslogd -i /var/run/syslogd.pid -c 5  

  10. root       983     1  0 Aug01 ?        00:00:00   /usr/sbin/sshd  

  11. root     17790   983  0 04:01 ?        00:00:00     sshd: root@pts/0   

  12. root     17795 17790  0 04:01 pts/0    00:00:00       -bash  

  13. root     17846 17795  0 04:14 pts/0    00:00:00         su - search  

  14. search   17847 17846  0 04:14 pts/0    00:00:00           -bash  

  15. search   17984 17847  0 04:51 pts/0    00:00:00             ps -efH  

  16. root      1077     1  0 Aug01 ?        00:00:00   /usr/libexec/postfix/master  

  17. postfix   1102  1077  0 Aug01 ?        00:00:00     qmgr -l -t fifo -u  

  18. postfix  17972  1077  0 04:29 ?        00:00:00     pickup -l -t fifo -u  

  19. root      1091     1  0 Aug01 ?        00:00:00   crond  

  20. root      1109     1  0 Aug01 tty1     00:00:00   /sbin/mingetty /dev/tty1  

  21. root      1111     1  0 Aug01 tty2     00:00:00   /sbin/mingetty /dev/tty2  

  22. root      1113     1  0 Aug01 tty3     00:00:00   /sbin/mingetty /dev/tty3  

  23. root      1115     1  0 Aug01 tty4     00:00:00   /sbin/mingetty /dev/tty4  

  24. root      1118     1  0 Aug01 tty5     00:00:00   /sbin/mingetty /dev/tty5  

  25. root      1122       



(2)實時監測進程信息,top命令 

Java代碼  收藏代碼

  1. [search@h1 ~]$ top  

  2. top - 04:52:35 up  5:23,  1 user,  load average: 0.000.000.00  

  3. Tasks:  73 total,   1 running,  72 sleeping,   0 stopped,   0 zombie  

  4. Cpu(s):  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st  

  5. Mem:   2913304k total,   183724k used,  2729580k free,    27532k buffers  

  6. Swap:  2031608k total,        0k used,  2031608k free,    64120k cached  

  7.   

  8.   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                              

  9.     7 root      20   0     0    0    0 S  0.3  0.0   0:15.84 events/0                                                                                                             

  10.     1 root      20   0 19232 1488 1220 S  0.0  0.1   0:01.06 init                                                                                                                 

  11.     2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd                                                                                                             

  12.     3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0                                                                                                          

  13.     4 root      20   0     0    0    0 S  0.0  0.0   0:00.02 ksoftirqd/0                                                                                                          

  14.     5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0                                                                                                          

  15.     6 root      RT   0     0    0    0 S  0.0  0.0   0:00.07 watchdog/0                                                                                                           

  16.     8 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup                                                                                                               

  17.     9 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper                                                                                                              

  18.    10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns                                                                                                                

  19.    11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr                                                                                                            

  20.    12 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm                                                                                                                   

  21.    13 root      20   0     0    0    0 S  0.0  0.0   0:00.47 sync_supers                                                                                                          

  22.    14 root      20   0     0    0    0 S  0.0  0.0   0:00.15 bdi-default                                                                                                          

  23.    15 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0                                                                                                        

  24.    16 root      20   0     0    0    0 S  0.0  0.0   0:00.20 kblockd/0                                                                                                            

  25.    17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid                                                                                                               

  26.    18 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify                                                                                                         

  27.    19 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug                                                                                                        

  28.    20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux                                                                                                              

  29.    21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_sff/0                                                                                                            

  30.    22 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd                                                                                                        

  31.    23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khubd                                                                                                                

  32.    24 root      20   0     0    0    0 S  0.0  0.0   0:00.01 kseriod                                                                                                              

  33.    25 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md/0                                                                                                                 

  34.    26 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md_misc/0                                                                                                            

  35.    27 root      20   0     0    0    0 S  0.0  0.0   0:00.00 linkwatch                                                                                                            

  36.    28 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khungtaskd                                                                                                           

  37.    29 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kswapd0                                                                                                              

  38.    30 root      25   5     0    0    0 S  0.0  0.0   0:00.00 ksmd                                                                                                                 

  39.    31 root      39  19     0    0    0 S  0.0  0.0   0:00.08 khugepaged                                                                                                           

  40.    32 root      20   0     0    0    0 S  0.0  0.0   0:00.00 aio/0           



平均負載,有3個值,最近1分鐘,最近5分鐘,和最近15分鐘的,值越大,負載越高,但如果15分鐘,負載都很高,說明系統可能出現問題了。 

在輸出中,第一行顯示了當前時間,系統的運行時間,登入的用戶數,和系統的平均負載 

第二行顯示了,概要的進程信息,top中輸出的進程叫task,顯示了多少個進程處於運行,休眠,停止,或殭屍進程(進程完成了,但父進程沒響應) 

第三行顯示了CPU信息,顯示了進程的狀態,運行,空間,等待等 

第四行顯示了系統的內存信息,總共內存,用了多少,多少空閒。 

第五行顯示了交換區內存的使用情況。 


(3),結束進程命令kill,也是實際使用中,非常重要的一個命令 

通常使用kill -9 進程號,來殺死一個進程,-9代表無條件停止。 

killall 命令,可以通過進程名,停掉進程,也可以使用通配的方式,停掉一組進程 
比如關閉apache的進程,可以使用命令killall http* 



(4)free -m命令,用來顯示當前系統的內存情況 

Java代碼  收藏代碼

  1. [search@h1 ~]$ free -m  

  2.              total       used       free     shared    buffers     cached  

  3. Mem:          2845        179       2665          0         27         62  

  4. -/+ buffers/cache:         89       2755  

  5. Swap:         1983          0       1983  

  6. [search@h1 ~]$   






(5)mount命令,掛載媒體,新增磁盤所需的命令。用法mount -t type directory 

Java代碼  收藏代碼

  1. [search@h1 ~]$ mount  

  2. /dev/mapper/vg_h1-lv_root on / type ext4 (rw)  

  3. proc on /proc type proc (rw)  

  4. sysfs on /sys type sysfs (rw)  

  5. devpts on /dev/pts type devpts (rw,gid=5,mode=620)  

  6. tmpfs on /dev/shm type tmpfs (rw)  

  7. /dev/sda1 on /boot type ext4 (rw)  

  8. none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)  

  9. [search@h1 ~]$   




移除一個移動設備的信息命令umount directory device 

(6)df命令,查詢磁盤詳細信息 

Java代碼  收藏代碼

  1. [search@h1 ~]$ df -h  

  2. Filesystem                 Size  Used Avail Use% Mounted on  

  3. /dev/mapper/vg_h1-lv_root   18G  3.6G   13G  22% /  

  4. tmpfs                      1.4G     0  1.4G   0% /dev/shm  

  5. /dev/sda1                  485M   32M  428M   7% /boot  

  6. [search@h1 ~]$   


-h 參數,可以用來轉換字節爲人類肉眼更加容易讀的形式,而不是字節。 

(7) du命令,顯示具體某個磁盤下,各個文件所佔的空間大小,使用這個命令,通常可以用來判斷某個磁盤下面是不是有超大文件。 

通常組合使用方法爲du -chs * 

Java代碼  收藏代碼

  1. [search@h1 ~]$ du -chs *  

  2. 138M    abc1.txt  

  3. 52M     apache-hive-0.13.1-bin.tar.gz  

  4. 4.0K    count.txt  

  5. 232K    filehivebak  

  6. 4.0K    formathadoop.sh  

  7. 380M    hadoop  

  8. 0       hbase  

  9. 308M    hbase-0.96.2-hadoop2  

  10. 76M     hbase-0.96.2-hadoop2-bin.tar.gz  

  11. 219M    hive  

  12. 1.2G    總用量  

  13. [search@h1 ~]$   



-c,顯示所有已列出文件的總的大小 
-h,按人類可讀的方式輸出大小 
-s,顯示每個輸出參數的總計 

散仙覺得這個命令單獨使用並沒有多大用處,一般都組合使用 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章