本篇散仙主要講一些,linux進程的管理,磁盤信息統計,CPU,內存等命令,非常實用。
實際開發中,linux系統的情況,能直接反映服務器運行的信息,cpu負載,內存佔用,磁盤等等,所以掌握這些命令,有助於我們更快更專業管理linux系統。
(1),ps命令,探查系統進程。
[search@h1 ~]$ ps
PID TTY TIME CMD
17847 pts/0 00:00:00 bash
17977 pts/0 00:00:00 ps
[search@h1 ~]$
默認情況下,ps命令不會顯示太多的信息,只顯示了pid進程號,和它運行在那個終端TTY,以及進程已用的CPU時間,和它執行的命令。
那麼,通常在實際開發中,通常會用ps -efl 命令,裏顯示更多的信息。
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 4808 poll_s Aug01 ? 00:00:01 /sbin/init
1 S root 2 0 0 80 0 - 0 kthrea Aug01 ? 00:00:00 [kthreadd]
1 S root 3 2 0 -40 - - 0 migrat Aug01 ? 00:00:00 [migration/0]
1 S root 4 2 0 80 0 - 0 ksofti Aug01 ? 00:00:00 [ksoftirqd/0]
1 S root 5 2 0 -40 - - 0 cpu_st Aug01 ? 00:00:00 [migration/0]
5 S root 6 2 0 -40 - - 0 watchd Aug01 ? 00:00:00 [watchdog/0]
1 S root 7 2 0 80 0 - 0 worker Aug01 ? 00:00:15 [events/0]
1 S root 8 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [cgroup]
1 S root 9 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [khelper]
1 S root 10 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [netns]
1 S root 11 2 0 80 0 - 0 async_ Aug01 ? 00:00:00 [async/mgr]
1 S root 12 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [pm]
1 S root 13 2 0 80 0 - 0 bdi_sy Aug01 ? 00:00:00 [sync_supers]
1 S root 14 2 0 80 0 - 0 bdi_fo Aug01 ? 00:00:00 [bdi-default]
1 S root 15 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kintegrityd/0]
1 S root 16 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kblockd/0]
1 S root 17 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kacpid]
1 S root 18 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kacpi_notify]
1 S root 19 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kacpi_hotplug]
1 S root 20 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [ata_aux]
1 S root 21 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [ata_sff/0]
1 S root 22 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [ksuspend_usbd]
5 S root 23 2 0 80 0 - 0 hub_th Aug01 ? 00:00:00 [khubd]
5 S root 24 2 0 80 0 - 0 serio_ Aug01 ? 00:00:00 [kseriod]
1 S root 25 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [md/0]
1 S root 26 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [md_misc/0]
1 S root 27 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [linkwatch]
1 S root 28 2 0 80 0 - 0 watchd Aug01 ? 00:00:00 [khungtaskd]
1 S root 29 2 0 80 0 - 0 kswapd Aug01 ? 00:00:00 [kswapd0]
1 S root 30 2 0 85 5 - 0 ksm_sc Aug01 ? 00:00:00 [ksmd]
1 S root 31 2 0 99 19 - 0 khugep Aug01 ? 00:00:00 [khugepaged]
1 S root 32 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [aio/0]
1 S root 33 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [crypto/0]
1 S root 38 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kthrotld/0]
1 S root 39 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [pciehpd]
1 S root 41 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kpsmoused]
1 S root 42 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [usbhid_resumer]
1 S root 73 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kstriped]
1 S root 145 2 0 80 0 - 0 scsi_e Aug01 ? 00:00:00 [scsi_eh_0]
1 S root 146 2 0 80 0 - 0 scsi_e Aug01 ? 00:00:00 [scsi_eh_1]
1 S root 153 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [mpt_poll_0]
1 S root 154 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [mpt/0]
1 S root 155 2 0 80 0 - 0 scsi_e Aug01 ? 00:00:00 [scsi_eh_2]
1 S root 295 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kdmflush]
1 S root 297 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [kdmflush]
1 S root 314 2 0 80 0 - 0 kjourn Aug01 ? 00:00:00 [jbd2/dm-0-8]
1 S root 315 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [ext4-dio-unwrit]
5 S root 389 1 0 76 -4 - 2769 poll_s Aug01 ? 00:00:00 /sbin/udevd -d
1 S root 408 2 0 80 0 - 0 bdi_wr Aug01 ? 00:00:00 [flush-253:0]
1 S root 602 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [vmmemctl]
1 S root 721 2 0 80 0 - 0 kjourn Aug01 ? 00:00:00 [jbd2/sda1-8]
1 S root 722 2 0 80 0 - 0 worker Aug01 ? 00:00:00 [ext4-dio-unwrit]
1 S root 765 2 0 80 0 - 0 kaudit Aug01 ? 00:00:00 [kauditd]
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
5 S root 941 1 0 80 0 - 62799 poll_s Aug01 ? 00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
5 S root 983 1 0 80 0 - 16651 poll_s Aug01 ? 00:00:00 /usr/sbin/sshd
4 S root 1077 1 0 80 0 - 20318 ep_pol Aug01 ? 00:00:00 /usr/libexec/postfix/master
1 S root 1091 1 0 80 0 - 29323 hrtime Aug01 ? 00:00:00 crond
4 S postfix 1102 1077 0 80 0 - 20380 ep_pol Aug01 ? 00:00:00 qmgr -l -t fifo -u
4 S root 1109 1 0 80 0 - 1016 n_tty_ Aug01 tty1 00:00:00 /sbin/mingetty /dev/tty1
4 S root 1111 1 0 80 0 - 1016 n_tty_ Aug01 tty2 00:00:00 /sbin/mingetty /dev/tty2
4 S root 1113 1 0 80 0 - 1016 n_tty_ Aug01 tty3 00:00:00 /sbin/mingetty /dev/tty3
4 S root 1115 1 0 80 0 - 1016 n_tty_ Aug01 tty4 00:00:00 /sbin/mingetty /dev/tty4
4 S root 1118 1 0 80 0 - 1016 n_tty_ Aug01 tty5 00:00:00 /sbin/mingetty /dev/tty5
4 S root 1122 1 0 80 0 - 1016 n_tty_ Aug01 tty6 00:00:00 /sbin/mingetty /dev/tty6
5 S root 1127 389 0 78 -2 - 2768 poll_s Aug01 ? 00:00:00 /sbin/udevd -d
5 S root 1128 389 0 78 -2 - 2768 poll_s Aug01 ? 00:00:00 /sbin/udevd -d
4 S root 17790 983 0 80 0 - 25099 poll_s 04:01 ? 00:00:00 sshd: root@pts/0
4 S root 17795 17790 0 80 0 - 27104 wait 04:01 pts/0 00:00:00 -bash
4 S root 17846 17795 0 80 0 - 36358 wait 04:14 pts/0 00:00:00 su - search
4 S search 17847 17846 0 80 0 - 27107 wait 04:14 pts/0 00:00:00 -bash
4 S postfix 17972 1077 0 80 0 - 20338 ep_pol 04:29 ? 00:00:00 pickup -l -t fifo -u
0 R search 17979 17847 0 80 0 - 27558 - 04:44 pts/0 00:00:00 ps -efl
[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參數,可以以樹狀的方式,顯示進程信息,我們可以很輕鬆的看到那些進程啓動了那些進程。
root 721 2 0 Aug01 ? 00:00:00 [jbd2/sda1-8]
root 722 2 0 Aug01 ? 00:00:00 [ext4-dio-unwrit]
root 765 2 0 Aug01 ? 00:00:00 [kauditd]
root 1 0 0 Aug01 ? 00:00:01 /sbin/init
root 389 1 0 Aug01 ? 00:00:00 /sbin/udevd -d
root 1127 389 0 Aug01 ? 00:00:00 /sbin/udevd -d
root 1128 389 0 Aug01 ? 00:00:00 /sbin/udevd -d
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
root 941 1 0 Aug01 ? 00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
root 983 1 0 Aug01 ? 00:00:00 /usr/sbin/sshd
root 17790 983 0 04:01 ? 00:00:00 sshd: root@pts/0
root 17795 17790 0 04:01 pts/0 00:00:00 -bash
root 17846 17795 0 04:14 pts/0 00:00:00 su - search
search 17847 17846 0 04:14 pts/0 00:00:00 -bash
search 17984 17847 0 04:51 pts/0 00:00:00 ps -efH
root 1077 1 0 Aug01 ? 00:00:00 /usr/libexec/postfix/master
postfix 1102 1077 0 Aug01 ? 00:00:00 qmgr -l -t fifo -u
postfix 17972 1077 0 04:29 ? 00:00:00 pickup -l -t fifo -u
root 1091 1 0 Aug01 ? 00:00:00 crond
root 1109 1 0 Aug01 tty1 00:00:00 /sbin/mingetty /dev/tty1
root 1111 1 0 Aug01 tty2 00:00:00 /sbin/mingetty /dev/tty2
root 1113 1 0 Aug01 tty3 00:00:00 /sbin/mingetty /dev/tty3
root 1115 1 0 Aug01 tty4 00:00:00 /sbin/mingetty /dev/tty4
root 1118 1 0 Aug01 tty5 00:00:00 /sbin/mingetty /dev/tty5
root 1122
(2)實時監測進程信息,top命令
[search@h1 ~]$ top
top - 04:52:35 up 5:23, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 73 total, 1 running, 72 sleeping, 0 stopped, 0 zombie
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
Mem: 2913304k total, 183724k used, 2729580k free, 27532k buffers
Swap: 2031608k total, 0k used, 2031608k free, 64120k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7 root 20 0 0 0 0 S 0.3 0.0 0:15.84 events/0
1 root 20 0 19232 1488 1220 S 0.0 0.1 0:01.06 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.07 watchdog/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cgroup
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khelper
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
13 root 20 0 0 0 0 S 0.0 0.0 0:00.47 sync_supers
14 root 20 0 0 0 0 S 0.0 0.0 0:00.15 bdi-default
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/0
16 root 20 0 0 0 0 S 0.0 0.0 0:00.20 kblockd/0
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpid
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_notify
19 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_hotplug
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_aux
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_sff/0
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksuspend_usbd
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd
24 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kseriod
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 md/0
26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 md_misc/0
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 linkwatch
28 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kswapd0
30 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
31 root 39 19 0 0 0 S 0.0 0.0 0:00.08 khugepaged
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命令,用來顯示當前系統的內存情況
[search@h1 ~]$ free -m
total used free shared buffers cached
Mem: 2845 179 2665 0 27 62
-/+ buffers/cache: 89 2755
Swap: 1983 0 1983
[search@h1 ~]$
(5)mount命令,掛載媒體,新增磁盤所需的命令。用法mount -t type directory
[search@h1 ~]$ mount
/dev/mapper/vg_h1-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
[search@h1 ~]$
移除一個移動設備的信息命令umount directory device
(6)df命令,查詢磁盤詳細信息
[search@h1 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_h1-lv_root 18G 3.6G 13G 22% /
tmpfs 1.4G 0 1.4G 0% /dev/shm
/dev/sda1 485M 32M 428M 7% /boot
[search@h1 ~]$
-h 參數,可以用來轉換字節爲人類肉眼更加容易讀的形式,而不是字節。
(7) du命令,顯示具體某個磁盤下,各個文件所佔的空間大小,使用這個命令,通常可以用來判斷某個磁盤下面是不是有超大文件。
通常組合使用方法爲du -chs *
[search@h1 ~]$ du -chs *
138M abc1.txt
52M apache-hive-0.13.1-bin.tar.gz
4.0K count.txt
232K filehivebak
4.0K formathadoop.sh
380M hadoop
0 hbase
308M hbase-0.96.2-hadoop2
76M hbase-0.96.2-hadoop2-bin.tar.gz
219M hive
1.2G 總用量
[search@h1 ~]$
-c,顯示所有已列出文件的總的大小
-h,按人類可讀的方式輸出大小
-s,顯示每個輸出參數的總計
散仙覺得這個命令單獨使用並沒有多大用處,一般都組合使用