实际开发中,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,显示每个输出参数的总计
散仙觉得这个命令单独使用并没有多大用处,一般都组合使用