小白畅通Linux之旅-----Linux进程管理

发布于:2025-05-30 ⋅ 阅读:(19) ⋅ 点赞:(0)

目录

一、进程查看命令

1、pstree

2、ps

3、pgrep

4、top、htop

二、进程管理命令

1、kill

2、pkill 和 killall

三、进程类型

1、前台进程

2、后台进程


一、进程查看命令

1、pstree

用于查看进程树之间的关系,谁是父进程,谁是子进程,可以清楚的看出来是谁创建了谁

用法:pstree [选项]

-A                  各进程树之间的连接以ASCll码字符来连接

[root@localhost ~]# pstree -A
systemd-+-ModemManager---2*[{ModemManager}]
        |-NetworkManager---2*[{NetworkManager}]
        |-VGAuthService
        |-accounts-daemon---2*[{accounts-daemon}]
        |-alsactl
        |-anacron
        |-at-spi-bus-laun-+-dbus-daemon---{dbus-daemon}
        |                 `-3*[{at-spi-bus-laun}]
        |-at-spi2-registr---2*[{at-spi2-registr}]
        |-atd
        |-auditd-+-sedispatch
        |        `-2*[{auditd}]
        |-avahi-daemon---avahi-daemon
        |-bluetoothd
        |-colord---2*[{colord}]
        |-crond
        |-cupsd
        |-dbus-daemon---{dbus-daemon}
        |-dconf-service---2*[{dcon

-P                  同时列出每个进程的PID

[root@localhost ~]# pstree -p
systemd(1)─┬─ModemManager(1203)─┬─{ModemManager}(1225)
           │                    └─{ModemManager}(1233)
           ├─NetworkManager(1639)─┬─{NetworkManager}(1695)
           │                      └─{NetworkManager}(1714)
           ├─VGAuthService(978)
           ├─accounts-daemon(993)─┬─{accounts-daemon}(1005)
           │                      └─{accounts-daemon}(1025)
           ├─alsactl(973)
           ├─anacron(42843)
           ├─at-spi-bus-laun(4502)─┬─dbus-daemon(4538)───{dbus-daemon}(4551)
           │                       ├─{at-spi-bus-laun}(4522)
           │                       ├─{at-spi-bus-laun}(4533)
           │                       └─{at-spi-bus-laun}(4535)
           ├─at-spi2-registr(4556)─┬─{at-spi2-registr}(4588)
           │                       └─{at-spi2-registr}(4590)

2、ps

显示系统当前进程瞬间运行状态

用法:ps  [选项]

-A                   所有进程均显示出来

[root@localhost ~]# ps -A
    PID TTY          TIME CMD
      1 ?        00:00:03 systemd
      2 ?        00:00:00 kthreadd
      3 ?        00:00:00 rcu_gp
      4 ?        00:00:00 rcu_par_gp
      5 ?        00:00:00 slub_flushwq
      7 ?        00:00:00 kworker/0:0H-events_highpri
     10 ?        00:00:00 mm_percpu_wq
     11 ?        00:00:00 rcu_tasks_rude_
     12 ?        00:00:00 rcu_tasks_trace
     13 ?        00:00:00 ksoftirqd/0
     14 ?        00:00:00 rcu_sched
     15 ?        00:00:00 migration/0
     16 ?        00:00:00 watchdog/0
     17 ?        00:00:00 cpuhp/0

-a                显示现行终端机下的所有进程,包括其他用户的进程

-u                以用户为主的进程状态

-x                通常与 a 这个参数一起使用,可列出较完整信息

ps -aux

#进程的所有者#进程id#进程占用的cpu百分比#进程占用的内存百分比#进程使用的虚拟内存大小#进程使用的物理内存大小#进程所在终端#进程状态#启动时间#运行时间#启动进程的命令

[root@localhost ~]# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.1  0.8 176204 14400 ?        Ss   17:26   0:03 /usr/lib/systemd/systemd --switched-roo
root           2  0.0  0.0      0     0 ?        S    17:26   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   17:26   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   17:26   0:00 [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<   17:26   0:00 [slub_flushwq]
root           7  0.0  0.0      0     0 ?        I<   17:26   0:00 [kworker/0:0H-events_highpri]
root          10  0.0  0.0      0     0 ?        I<   17:26   0:00 [mm_percpu_wq]
root          11  0.0  0.0      0     0 ?        S    17:26   0:00 [rcu_tasks_rude_]
root          12  0.0  0.0      0     0 ?        S    17:26   0:00 [rcu_tasks_trace]
root          13  0.0  0.0      0     0 ?        S    17:26   0:00 [ksoftirqd/0]
root          14  0.0  0.0      0     0 ?        I    17:26   0:00 [rcu_sched]
root          15  0.0  0.0      0     0 ?        S    17:26   0:00 [migration/0]
root          16  0.0  0.0      0     0 ?        S    17:26   0:00 [watchdog/0]
root          17  0.0  0.0      0     0 ?        S    17:26   0:00 [cpuhp/0]
root          18  0.0  0.0      0     0 ?        S    17:26   0:00 [cpuhp/1]
root          19  0.0  0.0      0     0 ?        S    17:26   0:00 [watchdog/1]

进程状态:

S:休眠

s:父进程

R:运行

Z:僵尸进程(前台程序已死,但后台驻留进程没有正常退出;后果就是占用资源不干活。)

3、pgrep

查看进程的信息,包括进程是否已经消亡,通过pgrep来获得正在被调度的进程的相关信息。 pgrep通过匹配其程序名,找到匹配的进程

[root@localhost ~]# pgrep nginx
42670
42671
42672
42673
42674

-l              同时显示进程名和PID

[root@localhost ~]# pgrep -l nginx
42670 nginx
42671 nginx
42672 nginx
42673 nginx
42674 nginx

-o             当匹配多个进程时,显示进程号最小的那个

[root@localhost ~]# pgrep -o nginx
42670

-n             当匹配多个进程时,显示进程号最大的那个

[root@localhost ~]# pgrep -n nginx
42674

注意:#pgrep命令用来查找进程的信息,通常会和kill命令来连用,在指定条件下kill问题进程

[root@localhost ~]# kill -9 `pgrep nginx`

4、top、htop

top:动态监控系统处理器状态,htop姑且称之为top的增强版

yum install epel-release -y    #安装epel源     
yum install htop -y     #安装htop

常用选项

-d              指定更新的时间间隔

-n              指定更新的次数

-u              监控指定用户的进程

-p              监控指定进程的PID

常用按键

空格键            立即刷新显示

M                    以内存占用百分比对进程进行排序

P                     以cpu占用百分比对进程进行排序

T                     按照进程运行时间对进程进行排序

q                     退出top命令

h                     显示帮助信息

`top -d 5`:每隔 5 秒更新一次进程信息。

`top -n 2`:更新两次进程信息后退出。

`top -p 1234`:监控进程 ID 为 1234 的进程。

`top -u root`:监控 root 用户的进程。

二、进程管理命令

1、kill

语法:kill   [选项]    pid

-l             列出所有信号参数

[root@localhost ~]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

-s          发送指定的信号

-9          强制杀死

-0          用来检测一个进程是否存在

[root@localhost ~]# ps -aux | grep nginx
root       45601  0.0  0.1 103476  2172 ?        Ss   14:10   0:00 nginx: master process /usr/sbin/nginx
nginx      45602  0.0  0.4 124876  7836 ?        S    14:10   0:00 nginx: worker process
nginx      45603  0.0  0.4 124876  7812 ?        S    14:10   0:00 nginx: worker process
nginx      45604  0.0  0.4 124876  7808 ?        S    14:10   0:00 nginx: worker process
nginx      45605  0.0  0.4 124876  7836 ?        S    14:10   0:00 nginx: worker process
root       45633  0.0  0.0 222016  1108 pts/1    S+   14:10   0:00 grep --color=auto nginx
[root@localhost ~]# kill -9 45601
[root@localhost ~]# ps -aux | grep nginx
root       45691  0.0  0.0 222016  1128 pts/1    S+   14:11   0:00 grep --color=auto nginx

2、pkill 和 killall

pkill是ps命令和kill命令的结合,按照进程名来杀死指定进程,pkill和killall应用方法差不多,也是直接杀死运行中的程序,如果想杀掉单个进程,请用kill来杀掉。

语法:pkill/killall [选项] 进程名
[root@localhost ~]# ps -aux | grep nginx
root       45750  0.0  0.1 103476  2176 ?        Ss   14:14   0:00 nginx: master process /usr/sbin/nginx
nginx      45751  0.0  0.4 124876  7996 ?        S    14:14   0:00 nginx: worker process
nginx      45752  0.0  0.4 124876  7996 ?        S    14:14   0:00 nginx: worker process
nginx      45753  0.0  0.4 124876  7996 ?        S    14:14   0:00 nginx: worker process
nginx      45754  0.0  0.4 124876  7996 ?        S    14:14   0:00 nginx: worker process
root       45817  0.0  0.0 222016  1100 pts/1    S+   14:14   0:00 grep --color=auto nginx
[root@localhost ~]# pkill nginx
[root@localhost ~]# ps -aux | grep nginx
root       45875  0.0  0.0 222016  1208 pts/1    S+   14:15   0:00 grep --color=auto nginx

三、进程类型

1、前台进程

在终端界面运行的进程,运行前台进程后,终端无法操作

(1)查看

(2)创建前台进程   nginx  -g   "daemon  off;"

[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# nginx -g "daemon off;"


[root@localhost ~]# ps -aux | grep nginx
root       46469  0.0  0.6 103480 12484 pts/1    S+   14:30   0:00 nginx: master process nginx -g daemon off;
nginx      46470  0.0  0.4 124876  7916 pts/1    S+   14:30   0:00 nginx: worker process
nginx      46471  0.0  0.4 124876  7896 pts/1    S+   14:30   0:00 nginx: worker process
nginx      46472  0.0  0.4 124876  7876 pts/1    S+   14:30   0:00 nginx: worker process
nginx      46473  0.0  0.4 124876  7884 pts/1    S+   14:30   0:00 nginx: worker process
root       46727  0.0  0.0 222016  1132 pts/2    S+   14:31   0:00 grep --color=auto nginx

(3)#将前台进程放到后台运行
           firefox & 
           #注意:该运行前台进程的方式,当终端关闭时,进程也会随着关闭
           nohup firefox & #关闭终端,进程不掉
           #将进程调到前台运行
           fg 1                ctrl + z 停止
           #将正在运行的前台进程放到后台运行
            bg 1

[root@localhost ~]# sleep 100 &
[1] 48585
[root@localhost ~]# ps -aux | grep sleep
root       48534  0.0  0.0 217160   848 ?        S    14:58   0:00 sleep 60
root       48585  0.0  0.0 217160   948 pts/1    S    14:59   0:00 sleep 100
root       48636  0.0  0.0 222016  1212 pts/1    S+   14:59   0:00 grep --color=auto sleep
[root@localhost ~]# fg 1
sleep 100

^Z
[1]+  已停止               sleep 100
[root@localhost ~]# ps -aux | grep sleep
root       48534  0.0  0.0 217160   848 ?        S    14:58   0:00 sleep 60
root       48585  0.0  0.0 217160   948 pts/1    T    14:59   0:00 sleep 100
root       48793  0.0  0.0 222016  1116 pts/1    S+   14:59   0:00 grep --color=auto sleep
[root@localhost ~]# bg 1
[1]+ sleep 100 &
[root@localhost ~]# ps -aux | grep sleep
root       48585  0.0  0.0 217160   948 pts/1    S    14:59   0:00 sleep 100
root       48876  0.0  0.0 217160   928 ?        S    14:59   0:00 sleep 60
root       48924  0.0  0.0 222016  1128 pts/1    S+   15:00   0:00 grep --color=auto sleep

2、后台进程

驻留在后端运行的进程