Linux进程管理

一、进程

  1、Linux最大的进程init,每个执行的程序(代码)都会产生一个进程,生成一个进程ID号

  2、每个进程都可能以两种方式存在,前台和后台。

二、查看进程PS命令

  1、ps -aux

  

   2、PS讲解

  

   

   3、查看父进程

  

   例子:查看sshd进程的父进程

  

 三、终止进程kill和killall

  1、基本语法

  

  2、常用选项

   -9:表示强迫进程立即停止

  3、案例实践

    1)踢掉某个非法登录用户

    

    2)终止远程登录服务sshd,在适当时候再次重启sshd服务

    

    3)终止多个gedit编译器【killall,通过进程名称来终止进程】

    

    4)强制杀掉一个终端,终端如果使用kill,系统会忽略,所以必须使用kill -9

    

四、查看进程树pstree

  1、基本语法

  pstree 【选项】,可以更加直观的来看进程信息

  2、常用选项

    -p:显示进程PID

    -u:显示进程的所属用户

  3、应用案例

    1)以树状的形式显示进程的pid

    

    2)以树状的形式显示进程的用户id

pstree -u

 五、service服务管理

  1、setup指令,查看所有服务和自启动状态

  

   2、查看系统服务

systemctl list-unit-files                                          查看所有服务
systemctl list-unit-files | grep enabled                           查看所有自启动服务
systemctl list-unit-files | grep disabled                          查看所有不自启动的服务

  3、服务的运行级别

  

   4、开机

  

   5、查看服务在各个运行级别中的状态 

[root@k8s-master ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:开    3:开    4:开    5:开    6:关

  6、查看某个服务

[root@k8s-master ~]# chkconfig --list|grep network

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

network            0:关    1:关    2:开    3:开    4:开    5:开    6:关

  7、设置某个服务自启

chkconfig --level 3 httpd  on   

六、动态监控进程

  1、top指令

[root@k8s-master ~]# top
top - 15:27:09 up 45 min,  1 user,  load average: 0.08, 0.05, 0.06
Tasks:  94 total,   1 running,  93 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.0 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  4030148 total,  3401492 free,   294932 used,   333724 buff/cache
KiB Swap:  3145724 total,  3145724 free,        0 used.  3484580 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                    
   633 kube      20   0  285780  51944  22732 S  0.3  1.3   0:08.03 kube-controller                                            
   901 etcd      20   0 10.353g  69204  11416 S  0.3  1.7   0:08.15 etcd                                                       
     1 root      20   0  125332   3820   2500 S  0.0  0.1   0:00.89 systemd                                                    
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                                                   
     3 root      20   0       0      0      0 S  0.0  0.0   0:00.08 ksoftirqd/0                                                
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                               
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.03 kworker/u256:0                                             
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                                                
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                                     
     9 root      20   0       0      0      0 S  0.0  0.0   0:00.21 rcu_sched                                                  
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 watchdog/0                                                 
    12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs                                                  
    13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns                                                      
    14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd

U:查看某个用户的进程
M:按照内存使用进行排序
d:代表秒数
原文地址:https://www.cnblogs.com/aqicheng/p/13883125.html