进程控制(二)与linux下的自有服务
一.進程動態信息查看top
第一部分 統計信息
[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分鐘內的平均負載,一般1以內的值比較合適,偏高說明有較多的進程在等待使用CPU資源
計算方法
平均負載 / 邏輯cpu數量
物理CPU(N路):主板上CPU插槽的個數
CPU核數:一塊CPU上面能處理數據的芯片組的數量
邏輯CPU:一般情況,一顆cpu可以有多核,加上intel的超線程技術(HT), 可以在邏輯上再分一倍數量的cpu core出來;邏輯CPU數量=物理cpu數量 x cpu核數。如果支持HT,還要更多。
查看物理CPU的個數
# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
查看邏輯CPU的個數
# cat /proc/cpuinfo |grep "processor"|wc -l
查看CPU是幾核
# cat /proc/cpuinfo |grep "cores"|uniq
CPU的運行狀況
us 用戶進程占用CPU的比率 sy 內核、內核進程占用CPU的比率; ni 如果一些用戶進程修改過優先級,這里顯示這些進程占用CPU時間的比率; id CPU空閑比率,如果系統緩慢而這個值很高,說明系統慢的原因不是CPU負載高; wa CPU等待執行I/O操作的時間比率,該指標可以用來排查磁盤I/O的問題,通常結合wa和id判斷 hi CPU處理硬件中斷所占時間的比率; si CPU處理軟件中斷所占時間的比率; st 其他任務所占CPU時間的比率;?
說明:
1. 用戶進程占比高,wa低,說明系統緩慢的原因在于進程占用大量CPU,通常還會伴有教低的id,說明CPU空閑時間很少;
2. wa低,id高,可以排除CPU資源瓶頸的可能。
3. wa高,說明I/O占用了大量的CPU時間,需要檢查交換空間的使用;如果內存充足,但wa很高,說明需要檢查哪個進程占用了大量的I/O資源。
第二部分 進程信息
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(數字) 顯示所有的CPU負載 M 按內存占用排序 P 按CPU的占用排序 T 按使用CPU的時間累積排序 R 逆序展示 k 向指定進程發出信號 s 設置多久刷新一次數據 r 設置優先級 q 退出top?
top常用選項
-d n 指定多久兩次刷屏的時間間隔為n秒
-p pid 只看進程號為pid的進程信息
-u 用戶名 指定查看某個用戶的進程
-b -n 以批處理方式執行top命令。通常使用數據流重定向,將處理結果輸出為文件;
?
二.進程控制
1.進程的優先級控制
(一)調整正在運行進程的優先級(renice)
(1)優先級范圍
-20--19,值越小,優先級越大,系統會給更多的CPU時間給該進程
(2)使用renice調整
renice 優先級值(-20-19) 進程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]#?
(二)程序運行時指定nice值
nice -n 優先級值(-20-19) 需要運行的進程==
啟動進程時,通常會繼承父進程的 nice級別,默認為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.進程的運行狀態控制
(一)如何控制進程的狀態
通過發送信號來控制進程的狀態
(二)常見的信號有哪些
| 信號編號 | 信號名 | 解釋說明 |
| -------- | ------- | ------------------------------------------------------ |
| 1 | SIGHUP | 默認終止控制終端進程(可用來重新加載配置文件,平滑重啟) |
| 2 | SIGINT | 鍵盤中斷(ctrl+c) |
| 3 | SIGQUIT | 鍵盤退出(ctrl+\),一般指程序異常產生core文件 |
| 9 | SIGKILL | 強制終止 |
| 15 | SIGTERM | 正常結束,默認信號 |
| 18 | SIGCONT | 繼續 |
| 19 | SIGSTOP | 停止 |
| 20 | SIGTSTP | 暫停(ctrl+z),一般子進程結束 |
(三)如何發送信號給進程
kill 信號 進程ID 該命令后面只能跟進程ID,不能跟進程名稱
pkill 信號 服務名稱 不能跟進程ID,
skill 信號 進程ID或服務名名稱都可以
killall 信號 服務名稱
?
(四)其他進程命令
&符號可以將進程放在后臺運行
[root@yunwei1 tmp]# sleep 300 & [1] 3506 [root@yunwei1 tmp]#jobs 查看后臺運行的進程
fg 將后臺的進程放到前臺執行
bg 將后臺進程放到后臺集訓執行
案例如下
[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?
三. 服務器的基本配置
1.主機名的配置
(一)查看主機名
hostname 命令可查看主機名
/etc/sysconfig/network 主機名配置文件
案例如下
[root@yunwei1 tmp]# hostname yunwei1.heima.cc[root@yunwei1 tmp]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=yunwei1.heima.cc [root@yunwei1 tmp]#?
(二)設置主機名
(1)臨時設置
hostname 要修改的主機名可以臨時設置,重啟后失效
[root@yunwei1 tmp]# hostname hehe.com [root@yunwei1 tmp]# hostname hehe.com [root@yunwei1 tmp]#?
(2)永久設置
就該配置文件,重啟系統后即可永久生效
[root@yunwei1 tmp]# vim /etc/sysconfig/network [root@yunwei1 tmp]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=yunwei1.heima.cc [root@yunwei1 tmp]#?
2.靜態IP的設置
(一)網卡配置文件位置
目錄/etc/sysconfig/network-scripts/下保存網絡相關命令,網卡配置信息文件
(二)網卡配置文件基本內容
[root@yunwei1 tmp]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 設備名稱 TYPE=Ethernet 以太網 ONBOOT=yes 是否開機自啟動 NM_CONTROLLED=yes 是否接受NetworkManger服務管理 BOOTPROTO=none 獲取IP方式,none和static是靜態獲取,dhcp是動態分配 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0" PEERDNS=yes PEERROUTES=yes LAST_CONNECT=1552268821 IPADDR=192.168.244.133 網卡的靜態ip地址 NETMASK=255.255.255.0 子網掩碼 GATEWAY=192.168.244.2 網關 DNS1=119.29.29.29 DNS服務器?
(三)網卡設置靜態IP
(1)文本圖形界面設置
setup命令
(2)修改配置文件設置
vim /etc/sysconfig/network-scripts/ifcfg-eth0
(四)網絡啟動/重啟
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 ]?
(五)其他網絡相關指令
查看網卡相關信息命令
#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
(一)查看防火墻狀態信息
service iptables status
或/etc/init.d/iptables status
或iptables -L 列出防火墻上的規則信息
(二)關閉防火墻
(1)臨時關閉
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)永久關閉
查看防火墻是否開機自啟動
#chkconfig --list |grep iptables
關閉防火墻開機自啟動
#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模式
查看當前系統的selinux運行模式
[root@yunwei1 ~]# getenforce Disabled當前系統的selinux運行模式可在/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模式表示安全級別最高,強制模式。
permissive模式表示警告模式,可以自由通過。
disabled表示沒有啟用selinux
?(四)關閉selinux
(1)臨時設置到高級模式
setenforce命令可以臨時設置 [root@yunwei1 ~]# setenforce usage: setenforce [ Enforcing | Permissive | 1 | 0 ] Permissive|0 高級模式[root@yunwei1 ~]# setenforce 0 setenforce: SELinux is disabled [root@yunwei1 ~]#?
(2)永久關閉
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.系統其它信息查看
(一)系統版本
[root@yunwei1 ~]# lsb_release -d Description: CentOS release 6.9 (Final) [root@yunwei1 ~]# [root@yunwei1 ~]# cat /etc/redhat-release CentOS release 6.9 (Final)?
(二)內核版本
顯示內核版本
[root@yunwei1 ~]# uname -r
2.6.32-696.el6.x86_64
顯示主機名
[root@yunwei1 ~]# uname -n
yunwei1.heima.cc
顯示內核名
[root@yunwei1 ~]# uname -s
Linux
[root@yunwei1 ~]# uname -i
x86_64
(三)磁盤掛載情況
查看當前已經掛在的磁盤信息
[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 ~]#
查看當前系統所有設備信息
?
(四)內存信息查看
free -m 選項-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 ~]#?
轉載于:https://www.cnblogs.com/golinux/p/10809017.html
總結
以上是生活随笔為你收集整理的进程控制(二)与linux下的自有服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 弗尤博客(十一)之搜索博文
- 下一篇: PyQt5菜单添加+事件+状态栏-7