zoukankan      html  css  js  c++  java
  • 进程控制(二)与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 cached

    load 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 kthreadd
    
    top常用按键说明
    1(数字) 显示所有的CPU负载
    M 按内存占用排序
    P 按CPU的占用排序
    T 按使用CPU的时间累积排序
    R 逆序展示
    k 向指定进程发出信号
    s 设置多久刷新一次数据
    r 设置优先级
    q 退出top

    top常用选项
    -d n 指定多久两次刷屏的时间间隔为n秒
    -p pid 只看进程号为pid的进程信息
    -u 用户名 指定查看某个用户的进程
    -b -n 以批处理方式执行top命令。通常使用数据流重定向,将处理结果输出为文件;

    案例如下
    [root@yunwei1 proc]# top -2
    top - 19:51:59 up 2:01, 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.3%sy, 0.0%ni, 99.2%id, 0.3%wa, 0.0%hi, 0.1%si, 0.0%st
    Mem: 1004112k total, 223996k used, 780116k free, 23280k buffers
    
    [root@yunwei1 proc]# top -d 2 -p 3198
    top - 19:53:42 up 2:03, 2 users, load average: 0.00, 0.00, 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.2%id, 0.3%wa, 0.0%hi, 0.1%si, 0.0%st
    Mem: 1004112k total, 225044k used, 779068k free, 23308k buffers
    Swap: 2031612k total, 0k used, 2031612k free, 88404k cached
    
    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
    3198 root 20 0 52132 812 260 S 0.0 0.1 0:00.00 vsftpd

    二.进程控制

    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 cached
    
    PID 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 proc]# kill -15 vsftpd
    -bash: kill: vsftpd: arguments must be process or job IDs
    [root@yunwei1 proc]# kill -15 3302
    [root@yunwei1 proc]# ps aux |grep vsftpd
    root 3342 0.0 0.0 103332 840 pts/0 S+ 20:13 0:00 grep vsftpd
    
    [root@yunwei1 proc]# pkill -15 3361
    [root@yunwei1 proc]# ps aux |grep vsftpd
    root 3361 0.0 0.0 52132 804 ? Ss 20:14 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
    root 3366 0.0 0.0 103332 844 pts/0 S+ 20:14 0:00 grep vsftpd
    [root@yunwei1 proc]# pkill -15 vsftpd
    [root@yunwei1 proc]# ps aux |grep vsftpd
    root 3371 0.0 0.0 103332 844 pts/0 S+ 20:14 0:00 grep vsftpd
    [root@yunwei1 proc]#
    
    [root@yunwei1 tmp]# ps aux|grep vsftpd
    root 3485 0.0 0.0 52132 808 ? Ss 20:24 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
    root 3495 0.0 0.0 103332 844 pts/0 S+ 20:24 0:00 grep vsftpd
    [root@yunwei1 tmp]# killall -15 3485
    3485: no process killed
    [root@yunwei1 tmp]# killall -15 vsftpd
    [root@yunwei1 tmp]# ps aux|grep vsftpd
    root 3499 0.0 0.0 103332 844 pts/0 S+ 20:24 0:00 grep vsftpd
    [root@yunwei1 tmp]# 

    (四)其他进程命令

    &符号可以将进程放在后台运行

    [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 ~]# 


    查看当前系统所有设备信息

    [root@yunwei1 ~]# lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sr0 11:0 1 3.7G 0 rom 
    sda 8:0 0 20G 0 disk 
    ├─sda1 8:1 0 500M 0 part /boot
    └─sda2 8:2 0 19.5G 0 part 
    ├─VolGroup-lv_root (dm-0) 253:0 0 17.6G 0 lvm /
    └─VolGroup-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]
    [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 ~]#
  • 相关阅读:
    java设计模式--桥接模式
    java设计模式--单例模式
    java设计模式--迭代器模式
    java设计模式--组合模式
    java设计模式--备忘录模式
    java设计模式--适配器模式
    洛谷P1464 Function
    洛谷P2434 [SDOI2005]区间
    p1416攻击火星
    p1359租用游艇
  • 原文地址:https://www.cnblogs.com/golinux/p/10809017.html
Copyright © 2011-2022 走看看