进程控制(二)与linux下的自有服务
一.進(jìn)程動態(tài)信息查看top
第一部分 統(tǒng)計信息
[root@yunwei1 ~]# top top - 19:22:52 up 1:32, 2 users, load average: 0.00, 0.00, 0.00 Tasks: 106 total, 1 running, 105 sleeping, 0 stopped, 0 zombie Cpu(s): 0.1%us, 0.1%sy, 0.0%ni, 99.6%id, 0.0%wa, 0.0%hi, 0.2%si, 0.0%st Mem: 1004112k total, 220464k used, 783648k free, 22328k buffers Swap: 2031612k total, 0k used, 2031612k free, 85752k cachedload average: 0.00, 0.00, 0.00為1分鐘,5分鐘,15分鐘內(nèi)的平均負(fù)載,一般1以內(nèi)的值比較合適,偏高說明有較多的進(jìn)程在等待使用CPU資源
計算方法
平均負(fù)載 / 邏輯cpu數(shù)量
物理CPU(N路):主板上CPU插槽的個數(shù)
CPU核數(shù):一塊CPU上面能處理數(shù)據(jù)的芯片組的數(shù)量
邏輯CPU:一般情況,一顆cpu可以有多核,加上intel的超線程技術(shù)(HT), 可以在邏輯上再分一倍數(shù)量的cpu core出來;邏輯CPU數(shù)量=物理cpu數(shù)量 x cpu核數(shù)。如果支持HT,還要更多。
查看物理CPU的個數(shù)
# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
查看邏輯CPU的個數(shù)
# cat /proc/cpuinfo |grep "processor"|wc -l
查看CPU是幾核
# cat /proc/cpuinfo |grep "cores"|uniq
CPU的運(yùn)行狀況
us 用戶進(jìn)程占用CPU的比率 sy 內(nèi)核、內(nèi)核進(jìn)程占用CPU的比率; ni 如果一些用戶進(jìn)程修改過優(yōu)先級,這里顯示這些進(jìn)程占用CPU時間的比率; id CPU空閑比率,如果系統(tǒng)緩慢而這個值很高,說明系統(tǒng)慢的原因不是CPU負(fù)載高; wa CPU等待執(zhí)行I/O操作的時間比率,該指標(biāo)可以用來排查磁盤I/O的問題,通常結(jié)合wa和id判斷 hi CPU處理硬件中斷所占時間的比率; si CPU處理軟件中斷所占時間的比率; st 其他任務(wù)所占CPU時間的比率;?
說明:
1. 用戶進(jìn)程占比高,wa低,說明系統(tǒng)緩慢的原因在于進(jìn)程占用大量CPU,通常還會伴有教低的id,說明CPU空閑時間很少;
2. wa低,id高,可以排除CPU資源瓶頸的可能。
3. wa高,說明I/O占用了大量的CPU時間,需要檢查交換空間的使用;如果內(nèi)存充足,但wa很高,說明需要檢查哪個進(jìn)程占用了大量的I/O資源。
第二部分 進(jìn)程信息
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4 root 20 0 0 0 0 S 1.9 0.0 0:00.09 ksoftirqd/0 1542 root 20 0 163m 7868 4476 S 1.9 0.8 0:11.51 vmtoolsd 1 root 20 0 19348 1556 1232 S 0.0 0.2 0:02.75 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreaddtop常用按鍵說明 1(數(shù)字) 顯示所有的CPU負(fù)載 M 按內(nèi)存占用排序 P 按CPU的占用排序 T 按使用CPU的時間累積排序 R 逆序展示 k 向指定進(jìn)程發(fā)出信號 s 設(shè)置多久刷新一次數(shù)據(jù) r 設(shè)置優(yōu)先級 q 退出top?
top常用選項(xiàng)
-d n 指定多久兩次刷屏的時間間隔為n秒
-p pid 只看進(jìn)程號為pid的進(jìn)程信息
-u 用戶名 指定查看某個用戶的進(jìn)程
-b -n 以批處理方式執(zhí)行top命令。通常使用數(shù)據(jù)流重定向,將處理結(jié)果輸出為文件;
?
二.進(jìn)程控制
1.進(jìn)程的優(yōu)先級控制
(一)調(diào)整正在運(yùn)行進(jìn)程的優(yōu)先級(renice)
(1)優(yōu)先級范圍
-20--19,值越小,優(yōu)先級越大,系統(tǒng)會給更多的CPU時間給該進(jìn)程
(2)使用renice調(diào)整
renice 優(yōu)先級值(-20-19) 進(jìn)程ID
[root@yunwei1 proc]# ps aux|grep vsftpd root 3198 0.0 0.0 52132 812 ? Ss 19:52 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf root 3214 0.0 0.0 103332 844 pts/0 S+ 19:56 0:00 grep vsftpd [root@yunwei1 proc]# renice -5 3198 3198: old priority 0, new priority -5 [root@yunwei1 proc]#?
(二)程序運(yùn)行時指定nice值
nice -n 優(yōu)先級值(-20-19) 需要運(yùn)行的進(jìn)程==
啟動進(jìn)程時,通常會繼承父進(jìn)程的 nice級別,默認(rèn)為0。
[root@yunwei1 proc]# nice -n -10 service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ] [root@yunwei1 proc]# ps aux |grep vsftpd root 3302 0.0 0.0 52132 808 ? S<s 20:01 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf root 3305 0.0 0.0 103332 844 pts/0 S+ 20:01 0:00 grep vsftpd [root@yunwei1 proc]# top -d 2 -p 3302 top - 20:02:06 up 2:12, 2 users, load average: 0.06, 0.02, 0.00 Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie Cpu(s): 0.1%us, 0.3%sy, 0.0%ni, 99.3%id, 0.3%wa, 0.0%hi, 0.1%si, 0.0%st Mem: 1004112k total, 225276k used, 778836k free, 23436k buffers Swap: 2031612k total, 0k used, 2031612k free, 88464k cachedPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3302 root 10 -10 52132 808 260 S 0.0 0.1 0:00.00 vsftpd?
2.進(jìn)程的運(yùn)行狀態(tài)控制
(一)如何控制進(jìn)程的狀態(tài)
通過發(fā)送信號來控制進(jìn)程的狀態(tài)
(二)常見的信號有哪些
| 信號編號 | 信號名 | 解釋說明 |
| -------- | ------- | ------------------------------------------------------ |
| 1 | SIGHUP | 默認(rèn)終止控制終端進(jìn)程(可用來重新加載配置文件,平滑重啟) |
| 2 | SIGINT | 鍵盤中斷(ctrl+c) |
| 3 | SIGQUIT | 鍵盤退出(ctrl+\),一般指程序異常產(chǎn)生core文件 |
| 9 | SIGKILL | 強(qiáng)制終止 |
| 15 | SIGTERM | 正常結(jié)束,默認(rèn)信號 |
| 18 | SIGCONT | 繼續(xù) |
| 19 | SIGSTOP | 停止 |
| 20 | SIGTSTP | 暫停(ctrl+z),一般子進(jìn)程結(jié)束 |
(三)如何發(fā)送信號給進(jìn)程
kill 信號 進(jìn)程ID 該命令后面只能跟進(jìn)程ID,不能跟進(jìn)程名稱
pkill 信號 服務(wù)名稱 不能跟進(jìn)程ID,
skill 信號 進(jìn)程ID或服務(wù)名名稱都可以
killall 信號 服務(wù)名稱
?
(四)其他進(jìn)程命令
&符號可以將進(jìn)程放在后臺運(yùn)行
[root@yunwei1 tmp]# sleep 300 & [1] 3506 [root@yunwei1 tmp]#jobs 查看后臺運(yùn)行的進(jìn)程
fg 將后臺的進(jìn)程放到前臺執(zhí)行
bg 將后臺進(jìn)程放到后臺集訓(xùn)執(zhí)行
案例如下
[root@yunwei1 tmp]# jobs [2]- Running sleep 3000 & [3]+ Running sleep 300 & [root@yunwei1 tmp]# [root@yunwei1 tmp]# ps aux|grep sleep root 3536 0.0 0.0 100928 572 pts/0 S 20:30 0:00 sleep 3000 root 3543 0.0 0.0 100928 572 pts/0 S 20:33 0:00 sleep 300 root 3548 0.0 0.0 103332 844 pts/0 S+ 20:34 0:00 grep sleep [root@yunwei1 tmp]# kill -19 3543 [3]+ Stopped sleep 300 [root@yunwei1 tmp]# jobs [2]- Running sleep 3000 & [3]+ Stopped sleep 300 [root@yunwei1 tmp]# [root@yunwei1 tmp]# bg %3 [3]+ sleep 300 & [root@yunwei1 tmp]# jobs [2]- Running sleep 3000 & [3]+ Running sleep 300 & [root@yunwei1 tmp]# [root@yunwei1 tmp]# fg %3 sleep 300?
三. 服務(wù)器的基本配置
1.主機(jī)名的配置
(一)查看主機(jī)名
hostname 命令可查看主機(jī)名
/etc/sysconfig/network 主機(jī)名配置文件
案例如下
[root@yunwei1 tmp]# hostname yunwei1.heima.cc[root@yunwei1 tmp]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=yunwei1.heima.cc [root@yunwei1 tmp]#?
(二)設(shè)置主機(jī)名
(1)臨時設(shè)置
hostname 要修改的主機(jī)名可以臨時設(shè)置,重啟后失效
[root@yunwei1 tmp]# hostname hehe.com [root@yunwei1 tmp]# hostname hehe.com [root@yunwei1 tmp]#?
(2)永久設(shè)置
就該配置文件,重啟系統(tǒng)后即可永久生效
[root@yunwei1 tmp]# vim /etc/sysconfig/network [root@yunwei1 tmp]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=yunwei1.heima.cc [root@yunwei1 tmp]#?
2.靜態(tài)IP的設(shè)置
(一)網(wǎng)卡配置文件位置
目錄/etc/sysconfig/network-scripts/下保存網(wǎng)絡(luò)相關(guān)命令,網(wǎng)卡配置信息文件
(二)網(wǎng)卡配置文件基本內(nèi)容
[root@yunwei1 tmp]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 設(shè)備名稱 TYPE=Ethernet 以太網(wǎng) ONBOOT=yes 是否開機(jī)自啟動 NM_CONTROLLED=yes 是否接受NetworkManger服務(wù)管理 BOOTPROTO=none 獲取IP方式,none和static是靜態(tài)獲取,dhcp是動態(tài)分配 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0" PEERDNS=yes PEERROUTES=yes LAST_CONNECT=1552268821 IPADDR=192.168.244.133 網(wǎng)卡的靜態(tài)ip地址 NETMASK=255.255.255.0 子網(wǎng)掩碼 GATEWAY=192.168.244.2 網(wǎng)關(guān) DNS1=119.29.29.29 DNS服務(wù)器?
(三)網(wǎng)卡設(shè)置靜態(tài)IP
(1)文本圖形界面設(shè)置
setup命令
(2)修改配置文件設(shè)置
vim /etc/sysconfig/network-scripts/ifcfg-eth0
(四)網(wǎng)絡(luò)啟動/重啟
service network start/restart 啟動/重啟
或/etc/init.d/network start/restart 啟動/重啟
或ifup eth0 啟動
案例如下
[root@yunwei1 tmp]# service network restart Shutting down interface eth0: Device state: 3 (disconnected) [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Active connection state: activated Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/2 [ OK ] [root@yunwei1 tmp]# /etc/init.d/network restart Shutting down interface eth0: Device state: 3 (disconnected) [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Active connection state: activated Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/3 [ OK ]?
(五)其他網(wǎng)絡(luò)相關(guān)指令
查看網(wǎng)卡相關(guān)信息命令
#ifconfig
#ip a
#ping ip地址 -c表示幾次
案例
[root@yunwei1 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4c:63:84 brd ff:ff:ff:ff:ff:ff inet 192.168.244.133/24 brd 192.168.244.255 scope global eth0 inet6 fe80::20c:29ff:fe4c:6384/64 scope link valid_lft forever preferred_lft forever [root@yunwei1 ~]# [root@yunwei1 ~]# ping -c2 192.168.31.21 PING 192.168.31.21 (192.168.31.21) 56(84) bytes of data. 64 bytes from 192.168.31.21: icmp_seq=1 ttl=128 time=1.06 ms 64 bytes from 192.168.31.21: icmp_seq=2 ttl=128 time=0.738 ms--- 192.168.31.21 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 0.738/0.900/1.062/0.162 ms [root@yunwei1 ~]#?
3.防火墻和selinux
(一)查看防火墻狀態(tài)信息
service iptables status
或/etc/init.d/iptables status
或iptables -L 列出防火墻上的規(guī)則信息
(二)關(guān)閉防火墻
(1)臨時關(guān)閉
service iptables stop
或/etc/init.d/iptables stop
案例
[root@yunwei1 ~]# service iptables start iptables: Applying firewall rules: [ OK ] [root@yunwei1 ~]# service iptables stop iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ]?
(2)永久關(guān)閉
查看防火墻是否開機(jī)自啟動
#chkconfig --list |grep iptables
關(guān)閉防火墻開機(jī)自啟動
#chkconfig iptables off
案例
[root@yunwei1 ~]# chkconfig --list |grep iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@yunwei1 ~]# [root@yunwei1 ~]# chkconfig --list |grep iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@yunwei1 ~]# chkconfig --level 35 iptables on [root@yunwei1 ~]# chkconfig --list |grep iptables iptables 0:off 1:off 2:off 3:on 4:off 5:on 6:off [root@yunwei1 ~]# chkconfig iptables off [root@yunwei1 ~]# chkconfig --list |grep iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@yunwei1 ~]#?
(三)查看selinux模式
查看當(dāng)前系統(tǒng)的selinux運(yùn)行模式
[root@yunwei1 ~]# getenforce Disabled當(dāng)前系統(tǒng)的selinux運(yùn)行模式可在/etc/selinux/config配置文件中查看
[root@yunwei1 ~]# cat /etc/selinux/config# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted注釋說明:
enforcing模式表示安全級別最高,強(qiáng)制模式。
permissive模式表示警告模式,可以自由通過。
disabled表示沒有啟用selinux
?(四)關(guān)閉selinux
(1)臨時設(shè)置到高級模式
setenforce命令可以臨時設(shè)置 [root@yunwei1 ~]# setenforce usage: setenforce [ Enforcing | Permissive | 1 | 0 ] Permissive|0 高級模式[root@yunwei1 ~]# setenforce 0 setenforce: SELinux is disabled [root@yunwei1 ~]#?
(2)永久關(guān)閉
vim /etc/selinux/config[root@yunwei1 ~]# vim /etc/selinux/config [root@yunwei1 ~]# cat /etc/selinux/config# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted配置文件修改后,需要下次重啟后才能生效
4.系統(tǒng)其它信息查看
(一)系統(tǒng)版本
[root@yunwei1 ~]# lsb_release -d Description: CentOS release 6.9 (Final) [root@yunwei1 ~]# [root@yunwei1 ~]# cat /etc/redhat-release CentOS release 6.9 (Final)?
(二)內(nèi)核版本
顯示內(nèi)核版本
[root@yunwei1 ~]# uname -r
2.6.32-696.el6.x86_64
顯示主機(jī)名
[root@yunwei1 ~]# uname -n
yunwei1.heima.cc
顯示內(nèi)核名
[root@yunwei1 ~]# uname -s
Linux
[root@yunwei1 ~]# uname -i
x86_64
(三)磁盤掛載情況
查看當(dāng)前已經(jīng)掛在的磁盤信息
[root@yunwei1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 5.4G 11G 34% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 36M 417M 8% /boot [root@yunwei1 ~]#
查看當(dāng)前系統(tǒng)所有設(shè)備信息
?
(四)內(nèi)存信息查看
free -m 選項(xiàng)-m值以mb為單位
[root@yunwei1 ~]# free -m total used free shared buffers cached Mem: 980 228 751 0 24 88 -/+ buffers/cache: 115 865 Swap: 1983 0 1983 [root@yunwei1 ~]#?
轉(zhuǎn)載于:https://www.cnblogs.com/golinux/p/10809017.html
總結(jié)
以上是生活随笔為你收集整理的进程控制(二)与linux下的自有服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 弗尤博客(十一)之搜索博文
- 下一篇: linux 时间同步ntp