一起来看管理进程

目录


一、查看和控制进程
内核功能:进程管理、内存管理、文件系统、网络功能、驱动程序、安全功能等
对于所有的操作系统,都有基本的功能

什么是程序?

保存在硬盘、光盘等介质中的可执行的代码和数据;

文件中静态保存的代码。

什么是进程?

在CPU及内存中运行的程序代码;

动态执行的代码;

父、子进程(每个程序可以创建一个或者多个进程)

进程是正在执行的程序

进程与线程的区别
资源分配与调度

    ·  进程是资源竞争的基本单位;
    ·  线程是程序执行的最小单位;

1.1ps命令——查看静态的进程统计信息
常用选项:
a :显示中断上的所有进程,包括其他用户的进程。
u:表示列出进程的用户。
x:显示所有终端的进程。

查看当前系统的进程

[root@localhost ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3 128164  6824 ?        Ss   15:43   0:00 /usr/lib/systemd/system
root          2  0.0  0.0      0     0 ?        S    15:43   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    15:43   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   15:43   0:00 [kworker/0:0H]
root          6  0.0  0.0      0     0 ?        S    15:43   0:00 [kworker/u256:0]
root          7  0.0  0.0      0     0 ?        S    15:43   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    15:43   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    15:43   0:00 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S    15:43   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    15:43   0:00 [kdevtmpfs]
root         13  0.0  0.0      0     0 ?        S<   15:43   0:00 [netns]
root         14  0.0  0.0      0     0 ?        S    15:43   0:00 [khungtaskd]
root         15  0.0  0.0      0     0 ?        S<   15:43   0:00 [writeback]
root         16  0.0  0.0      0     0 ?        S<   15:43   0:00 [kintegrityd]
root         17  0.0  0.0      0     0 ?        S<   15:43   0:00 [bioset]
root         18  0.0  0.0      0     0 ?        S<   15:43   0:00 [kblockd]
root         19  0.0  0.0      0     0 ?        S<   15:43   0:00 [md]
root         25  0.0  0.0      0     0 ?        S    15:43   0:00 [kswapd0]
root         26  0.0  0.0      0     0 ?        SN   15:43   0:00 [ksmd]
root         27  0.0  0.0      0     0 ?        SN   15:43   0:00 [khugepaged
[root@localhost ~]# ps -T
   PID   SPID TTY          TIME CMD
  2815   2815 pts/1    00:00:00 bash
  3545   3545 pts/1    00:00:00 ps

查看当前线程

USER:启动该进程的用户账号名称
PID:进程的ID
%CPU:进程占用的CPU百分比
%MEM:占用内存的百分比
Vsz:该进程使用的虚拟内存量(KB)
RSS:该进程占用的物理内存量(KB)
TTY:启动进程的终端名。不是从终端启动的进程则显示为?
STAT:该进程的状态。
START:该进程被触发启动时间
TIME:该进程实际使用CPU运行的时间
COMMAND:进程的启动命令

STAT:
D:不可被唤醒的休眠状态;
R:正在运行状态
S:处于休眠状态,可被唤醒;
T:停止状态,可能时在后台暂停或进程处于跟踪调试状态;
Z:僵尸进程。进程已经终止,但是部分程序还在内存当中,但器父进程无法正常终止它

僵厂进程: 僵尸进程是当子进程比父进程先结束,而父进程又没有回收子进程,释放子进程占用的资源,此时子进程将成为一个僵尸进程。如果父进程先退出 ,子进程被init接管,子进程退出后init会回收其占用的相关资源

1.2 ps -elf命令——查看静态的进程统计信息
-e:显示系统内的所有进程信息
-l:使用长格式显示进程信息
-f:使用完整的格式显示进程信息

[root@localhost ~]# ps -elf
F S UID         PID   PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root          1      0  0  80   0 - 32041 ep_pol 15:43 ?        00:00:00 /usr/lib/sys
1 S root          2      0  0  80   0 -     0 kthrea 15:43 ?        00:00:00 [kthreadd]
1 S root          3      2  0  80   0 -     0 smpboo 15:43 ?        00:00:00 [ksoftirqd/0
1 S root          5      2  0  60 -20 -     0 worker 15:43 ?        00:00:00 [kworker/0:0
1 S root          6      2  0  80   0 -     0 worker 15:43 ?        00:00:00 [kworker/u25
1 S root          7      2  0 -40   - -     0 smpboo 15:43 ?        00:00:00 [migration/0
1 S root          8      2  0  80   0 -     0 rcu_gp 15:43 ?        00:00:00 [rcu_bh]
1 R root          9      2  0  80   0 -     0 -      15:43 ?        00:00:00 [rcu_sched]
5 S root         10      2  0 -40   - -     0 smpboo 15:43 ?        00:00:00 [watchdog/0]
5 S root         12      2  0  80   0 -     0 devtmp 15:43 ?        00:00:00 [kdevtmpfs]
1 S root         13      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [netns]
1 S root         14      2  0  80   0 -     0 watchd 15:43 ?        00:00:00 [khungtaskd]
1 S root         15      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [writeback]
1 S root         16      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [kintegrityd
1 S root         17      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [bioset]
1 S root         18      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [kblockd]
1 S root         19      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [md]
1 S root         25      2  0  80   0 -     0 kswapd 15:43 ?        00:00:00 [kswapd0]
1 S root         26      2  0  85   5 -     0 ksm_sc 15:43 ?        00:00:00 [ksmd]
1 S root         27      2  0  99  19 -     0 khugep 15:43 ?        00:00:00 [khugepaged]
1 S root         28      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [crypto]
1 S root         36      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [kthrotld]
1 S root         37      2  0  80   0 -     0 worker 15:43 ?        00:00:00 [kworker/u25
1 S root         38      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [kmpath_rdac
1 S root         39      2  0  60 -20 -     0 rescue 15:43 ?        00:00:00 [kpsmoused]

F:内核分配给进程的系统标记
s:进程的状态
UID: 启动这些进程的用户
PID:进程的进程ID,每个进程都有唯一的PID
PPID:父进程进程号(如果该进程时由另一个进程启动的)
C:进程生命周期中的CPU利用率
PRI:进程的优先级(越大的数字代表越低的优先级)
NI:谦让度值用来参与决定优先级 ADDR:进程的内存地址
sz:假如进程被换出,所需交换空间的大致大小
WCHAN:若该进程在睡眠,则显示睡眠中的系统函数名
STIME:进程启动时的系统时间
TTY:进程启动的终端设备
TIME:运行进程需要的累计CPU时间
CMD:进程的启动命令

1.3 grep过滤查询
执行以下操作可以过滤出包含“bash”的进程信息

[root@localhost ~]# ps aux | grep bash
root        779  0.0  0.0 115256   924 ?        S    15:43   0:00 /bin/bash /usr/sbin/ksmtuned
root       1896  0.0  0.0  51332   588 ?        Ss   15:44   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
root       2683  0.0  0.1 116688  3448 pts/0    Ss+  15:44   0:00 bash
root       2815  0.0  0.1 116576  3404 pts/1    Ss   15:47   0:00 -bash
root       3802  0.0  0.0 112676   984 pts/1    R+   17:27   0:00 grep --color=auto bash

[root@localhost ~]# ps -elf | grep bash
1 S root        779      1  0  80   0 - 28814 do_wai 15:43 ?        00:00:00 /bin/bash /usr/sbin/ksmtuned
1 S root       1896   1733  0  80   0 - 12833 poll_s 15:44 ?        00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
4 S root       2683   2676  0  80   0 - 29172 n_tty_ 15:44 pts/0    00:00:00 bash
4 S root       2815   2803  0  80   0 - 29144 do_wai 15:47 pts/1    00:00:00 -bash
0 R root       3816   2815  0  80   0 - 28169 -      17:28 pts/1    00:00:00 grep --color=auto bash

1.4top命令——动态查看进程信息

动态查看进程

在这里插入图片描述
shift+m(内存排序)
在这里插入图片描述
查看当前进程

在这里插入图片描述

1.5pgrep命令——根据特定条件查询进程PID信息

-l:显示进程名
-U:指定特定用户
-t:指定终端

[root@localhost ~]# pgrep -l "log"
401 xfs-log/dm-0
584 xfs-log/sda1
686 systemd-logind
716 abrt-watch-log
717 abrt-watch-log
727 rsyslogd
[root@localhost ~]# pgrep -l -U root -t tty1
1509 X

1.6pstree命令——查看进程树
pstree命令可以查看当前系统的进程树,包括个各进程对应的PID号、用户名、完整命令等信息

[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─alsactl
        ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───2*[{at-spi2-registr}]
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─bluetoothd
        ├─chronyd
        ├─colord───2*[{colord}]
        ├─crond
        ├─cupsd
        ├─2*[dbus-daemon───{dbus-daemon}]
        ├─dbus-launch
        ├─dconf-service───2*[{dconf-service}]
        ├─dnsmasq───dnsmasq
        ├─evolution-addre─┬─evolution-addre───5*[{evolution-addre}]
        │                 └─5*[{evolution-addre}]
        ├─evolution-calen─┬─evolution-calen───5*[{evolution-calen}]
        │                 ├─evolution-calen───7*[{evolution-calen}]
        │                 └─5*[{evolution-calen}]
        ├─evolution-sourc───4*[{evolution-sourc}]
        ├─firewalld───{firewalld}
        ├─gdm─┬─X───{X}
        │     ├─gdm-session-wor─┬─gnome-session-b─┬─abrt-applet───2*[{abrt-applet}]
        │     │                 │                 ├─gnome-settings-───4*[{gnome-settings+
        │     │                 │                 ├─gnome-shell─┬─ibus-daemon─┬─ibus-dco+
        │     │                 │                 │             │             ├─ibus-eng+
        │     │                 │                 │             │             └─2*[{ibus+
        │     │                 │                 │             └─6*[{gnome-shell}]
        │     │                 │                 ├─gnome-software───3*[{gnome-software}+
        │     │                 │                 ├─nautilus-deskto───3*[{nautilus-deskt+
        │     │                 │                 ├─seapplet───2*[{seapplet}]
        │     │                 │                 ├─ssh-agent
        │     │                 │                 ├─tracker-extract───13*[{tracker-extra+
        │     │                 │                 ├─tracker-miner-a───3*[{tracker-miner-+
        │     │                 │                 ├─tracker-miner-f───3*[{tracker-miner-+
        │     │                 │                 ├─tracker-miner-u───3*[{tracker-miner-+
        │     │                 │                 └─3*[{gnome-session-b}]
        │     │                 └─2*[{gdm-session-wor}]
        │     └─3*[{gdm}]
        ├─gnome-keyring-d───3*[{gnome-keyring-d}]
        ├─gnome-shell-cal───6*[{gnome-shell-cal}]
        ├─gnome-terminal-─┬─bash
        │                 ├─gnome-pty-helpe
        │                 └─3*[{gnome-terminal-}]
        ├─goa-daemon───4*[{goa-daemon}]
        ├─goa-identity-se───3*[{goa-identity-se}]
        ├─gsd-printer───2*[{gsd-printer}]
        ├─gssproxy───5*[{gssproxy}]
        ├─gvfs-afc-volume───3*[{gvfs-afc-volume}]
        ├─gvfs-goa-volume───2*[{gvfs-goa-volume}]
        ├─gvfs-gphoto2-vo───2*[{gvfs-gphoto2-vo}]
        ├─gvfs-mtp-volume───2*[{gvfs-mtp-volume}]
        ├─gvfs-udisks2-vo───2*[{gvfs-udisks2-vo}]
        ├─gvfsd───2*[{gvfsd}]
        ├─gvfsd-fuse───5*[{gvfsd-fuse}]
        ├─gvfsd-metadata───2*[{gvfsd-metadata}]
        ├─gvfsd-trash───2*[{gvfsd-trash}]
        ├─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │             └─2*[{ibus-daemon}]
        ├─2*[ibus-x11───2*[{ibus-x11}]]
        ├─ksmtuned───sleep
        ├─libvirtd───15*[{libvirtd}]
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mission-control───3*[{mission-control}]
        ├─packagekitd───2*[{packagekitd}]
        ├─polkitd───5*[{polkitd}]
        ├─pulseaudio───2*[{pulseaudio}]
        ├─rngd
        ├─rsyslogd───2*[{rsyslogd}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─smartd
        ├─sshd───sshd───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tracker-store───7*[{tracker-store}]
        ├─tuned───4*[{tuned}]
        ├─udisksd───4*[{udisksd}]
        ├─upowerd───2*[{upowerd}]
        ├─vmtoolsd───{vmtoolsd}
        ├─vmtoolsd
        ├─wpa_supplicant
        └─xdg-permission-───2*[{xdg-permission-}]

-A:各进程树之间的连接以ASCll字符来连接
-u:各进程树之间的连接以Unicode字符来连接,在某些终端界面下可能会有错误
-p:同时列出每个进程的PID
-u:同时列出每个进程的所属账号名称

在这里插入图片描述

查看特定的用户进程树,不需要u,因为已经指定了yxp用户
在这里插入图片描述
1.6控制进程
进程启动方式为手工启动和调度启动
(1)手动启动
前台启动:用户输入命令,直接执行的程序;
后台启动:在命令行为加入”&“符号

当使用cp命令从光盘中制作镜像文件时,由于需要复制的数据较多,耗时较长,因此可结合“&”符号将复制操作放到后台运行,以便用户可以继续执行其他命令操作

[root@localhost ~]# vim c &
[1] 4132
[root@localhost ~]# cp /dev/cdrom mycd.iso &
[2] 4151

[1]+  已停止               vim c

(2)进程的前后台调度(Ctrl+Z组合键、job命令和fg命令)

Ctrl + Z 组合键: 将当前进程挂起,即调入后台并停止执行
jobs命令:查看处于后台的任务列表
[-l]选项显示PID号
fg命令:将后台进程恢复到前台运行,可指定任务序列号

在这里插入图片描述
1.7终止程序的运行
Ctrl+C组合键:终端正在执行的命令;
kill、killall命令
kill用于终止指定PID号的进程
killall用于终止指定名称相关的所有进程
-9选项用于强制终止

pkill命令:根据特定条件终止相应的进程
-U:根据进程所属的用户名终止相应进程
-t:根据进程所在的终端终止相应进程

二、计划任务管理
2.1使用at命令设置一次性计划任务
在这里插入图片描述
实例:在当天的21:30时自动关闭当前系统
在这里插入图片描述
2.2使用crontab命令,设置周期性计划任务、、
(1)crontab命令简介
crontab命令:

按照预先设置的时间周期(分钟、小时、天…)重复执行用户指定的命令操作 >属于周期性计划任务
服务脚本名称:/etc/init.d/crond

查看配置文件

[root@localhost ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed


(2)crontab任务配置的格式
时间数值特殊表示方法
*表示该范围内的任意书剑
,表示间隔的多个不连续时间点
-表示一个连续的时间范围
/指定间隔的时间频率

编辑计划任务

crontab -e [-u 用户名] #-u缺省时默认是针对当前用户,只有超级用户才可以用-u

查看计划任务
crontab -l [-u 用户名]

删除计划任务
crontab -r [-u 用户名]

在这里插入图片描述
在这里插入图片描述

三、总结
1.查看进程的命令:ps aux 或者ps -elf;top;pgrep;pstree;
2.启动进程:前台启动;后台启动(命令最后加&)
查看后台任务列表:jobs
调用后台进程:fg 任务列表号
ctrl+z挂起
kill -9 进程号 PID
pkill -9 进程号 PID
killall 全部进程
at 小时:分钟 年-月-日
命令:ctrl D保存运行
atq 序号
atrm +序号
3.掌握程序、进程、线程
4.查看进程的命令:ps aux 或者ps -elf;top;pgrep;pstree;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老赵学coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值