Linux系统——进程操作
一、进程查看
进程(process), 系统进行资源分配的最小单位
1、ps命令的使用
// 查看所有进程
[root@localhost ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 190928 2060 ? Ss 10:47 0:01 /usr/lib/systemd/systemd --switched-ro
root 2 0.0 0.0 0 0 ? S 10:47 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 10:47 0:00 [kworker/0:0H]
COMMAND:进程名称
START:进程启动时间
USER:进程启动的用户
PID:进程ID
%CPU, %MEM:进程所消耗的CPU、内存
[root@localhost ~]# ps aux | grep ping
root 10645 0.0 0.1 128556 1276 pts/1 S+ 14:18 0:00 ping 10.11.0.254
root 10647 0.0 0.0 112812 980 pts/0 S+ 14:18 0:00 grep --color=auto ping
[root@localhost ~]# ps aux | grep vim
root 10695 0.2 0.5 149684 5388 pts/2 S+ 14:19 0:00 vim /etc/fstab
root 10697 0.0 0.0 112812 980 pts/0 S+ 14:19 0:00 grep --color=auto vim
- VSZ、RSS【关注】
说明进程所消耗的内存
VSZ:虚拟内存集, 包括物理内存 + swap + 共享内存
RSS:实际物理内存大小
2、查看内存
// 查看内存
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 972 137 477 1 357 697
Swap: 2047 392 1655
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 972M 136M 478M 2.0M 357M 697M
Swap: 2.0G 392M 1.6G
3、进程状态
cpu按时间切片的方式执行多个进程,进程才会状态之分
R:运行, D/S:睡眠, +:前台
- 异常状态
Z:僵尸状态,子进程销毁时,资源没有被正常回收
T:停止状态
// PPID:父进程ID
[root@localhost ~]# ps -elf | grep nginx
1 S root 13508 1 0 80 0 - 5141 sigsus 16:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nobody 13519 13508 0 80 0 - 5250 ep_pol 16:30 ? 00:00:00 nginx: worker process
0 S root 13525 1255 0 80 0 - 28203 pipe_w 16:32 pts/0 00:00:00 grep --color=auto nginx
// 将父进程挂起,处于停止状态
[root@localhost ~]# kill -19 13508
[root@localhost ~]# ps -elf | grep nginx
1 T root 13508 1 0 80 0 - 5141 do_sig 16:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nobody 13519 13508 0 80 0 - 5250 ep_pol 16:30 ? 00:00:00 nginx: worker process
0 S root 13527 1255 0 80 0 - 28203 pipe_w 16:32 pts/0 00:00:00 grep --color=auto nginx
// 结束子进程
[root@localhost ~]# kill 13519
[root@localhost ~]#
[root@localhost ~]# ps -elf | grep nginx
1 T root 13508 1 0 80 0 - 5141 do_sig 16:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 Z nobody 13519 13508 0 80 0 - 0 do_exi 16:30 ? 00:00:00 [nginx] <defunct>
0 S root 13529 1255 0 80 0 - 28203 pipe_w 16:33 pts/0 00:00:00 grep --color=auto nginx
// 恢复父进程的运行, 回收僵尸进程
[root@localhost ~]# kill -18 13508
[root@localhost ~]#
[root@localhost ~]# ps -elf | grep nginx
1 S root 13508 1 0 80 0 - 5141 sigsus 16:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nobody 13532 13508 0 80 0 - 5250 ep_pol 16:36 ? 00:00:00 nginx: worker process
0 S root 13534 1255 0 80 0 - 28203 pipe_w 16:36 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]#
- 查看进程树
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─VGAuthService
├─agetty
├─auditd───{auditd}
├─crond
├─dbus-daemon
├─irqbalance
├─lvmetad
├─master─┬─pickup
│ └─qmgr
二、进程调度/控制
1、控制进程前/后台运行
默认情况下,所有指令都是在前台运行(占着命令提示符 )
- 将命令放入后台运行
# nohup 命令 &
[root@localhost ~]# nohup sleep 60 &
[1] 1349
[root@localhost ~]# nohup: ignoring input and appending output to ‘nohup.out’
// 查看后台的任务
[root@localhost ~]# jobs -l
[1]+ 1349 Running nohup sleep 60 &
- 将任务调回前台运行
[root@localhost ~]# fg 1
nohup sleep 90
- 将正在运行的任务放入后台暂停执行
ctrl + z
[root@localhost ~]# sleep 300
^Z
[1]+ Stopped sleep 300
[root@localhost ~]# jobs -l
[1]+ 1381 Stopped sleep 300
// 将后台暂停的任务继续执行
[root@localhost ~]# bg 1
[1]+ sleep 300 &
[root@localhost ~]# jobs -l
[1]+ 1381 Running sleep 300 &
2、向进程发送信号
- 发送信号
# kill [信号] PID
默认是结束信号
- 查看信号
[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
- 结束/杀死进程
// 信号15: 正常结束
[root@localhost ~]# ps -elf | grep ping
4 S root 1435 1414 0 80 0 - 32139 skb_wa 09:38 pts/1 00:00:00 ping 10.11.0.254
0 S root 1437 1298 0 80 0 - 28203 pipe_w 09:38 pts/0 00:00:00 grep --color=auto ping
[root@localhost ~]# kill 1435
[root@localhost ~]# ps -elf | grep ping
4 S root 1461 1414 0 80 0 - 32139 poll_s 09:39 pts/1 00:00:00 ping 10.11.0.254
0 S root 1463 1298 0 80 0 - 28203 pipe_w 09:39 pts/0 00:00:00 grep --color=auto ping
// 信号9:强制杀死
[root@localhost ~]# kill -9 1461
三、计划任务
1、一次性任务
- 前提: 依赖于atd服务正常运行
[root@localhost ~]# rpm -q at
at-3.1.13-25.el7_9.x86_64
[root@localhost ~]# systemctl status atd
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-10-19 10:29:12 CST; 8s ago
Main PID: 1708 (atd)
CGroup: /system.slice/atd.service
└─1708 /usr/sbin/atd -f
Oct 19 10:29:12 localhost.localdomain systemd[1]: Started Job spooling tools.
- 创建一次性任务
[root@localhost ~]# at 10:32
at> rm -rf /tmp/*
at> <EOT>
job 1 at Sat Oct 19 10:32:00 2024
// 查看一次性任务
[root@localhost ~]# at -l
1 Sat Oct 19 10:32:00 2024 a root
2、周期性任务
- 前提: 依赖于crond服务正常运行
[root@localhost ~]# systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-10-19 09:03:20 CST; 1h 31min ago
Main PID: 718 (crond)
CGroup: /system.slice/crond.service
└─718 /usr/sbin/crond -n
Oct 19 09:03:20 localhost.localdomain systemd[1]: Started Command Scheduler.
Oct 19 09:03:20 localhost.localdomain crond[718]: (CRON) INFO (RANDOM_DELAY will be scaled with fa...d.)
Oct 19 09:03:20 localhost.localdomain crond[718]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.
- 创建周期性任务
[root@localhost ~]# crontab -e
- 时间的写法
时间的写法:
分 时 日 月 周
每天晚上十一点半 30 23 * * *
每周五早上6点 0 6 * * 5
使用逗号,表示不连续的时间
每周1,周3, 周5下午4点半 30 16 * * 1,3,5
每天早上8点半,10点半,12点半 30 8,10,12 * * *
使用横杠-表示连续的时间
30 8-16 * * *
0 22 * * 2-4
/: 每隔多少时间
每隔半小时 */30 * * * *
上午10点到下午6点,每隔2个小时 0 10-18/2 * * *
- 操作的注意事项
1、建议写命令的全路径 【针对源码软件的命令】
[root@localhost ~]# head -n 2 /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
2、%默认识别为命令的结束符,需要转义, %
- 需求1:每分钟创建测试文件
[root@localhost ~]# crontab -l
*/1 * * * * /usr/bin/touch /test/file_$(date +\%F_\%T)
- 需求2: 每分钟同步一次时间
// 计划任务默认会把命令的信息作为邮件发送给root, &> /dev/null 重定向黑洞文件,避免发送邮件
[root@localhost ~]# crontab -l
*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null
四、性能查看工具
1、top
top - 15:12:59 up 6:09, 1 user, load average: 0.02, 0.02, 0.13
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.5 us, 0.0 sy, 0.0 ni, 98.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 995704 total, 161936 free, 512096 used, 321672 buff/cache
KiB Swap: 2097148 total, 2096884 free, 264 used. 328868 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 125528 3684 2212 S 0.0 0.4 0:01.90 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
4 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.12 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.10 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:01.02 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:00.09 watchdog/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.08 watchdog/1
13 root rt 0 0 0 0 S 0.0 0.0 0:00.11 migration/1
14 root 20 0 0 0 0 S 0.0 0.0 0:00.11 ksoftirqd/1
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H
17 root rt 0 0 0 0 S 0.0 0.0 0:00.07 watchdog/2
- 动态显示进程
M:按内存占比,倒序显示进程
P:按CPU占比,倒序显示进程
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 125528 3684 2212 S 0.0 0.4 0:01.90 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
- 查看系统负载/CPU负载
top - 15:12:59 up 6:09, 1 user, load average: 0.02, 0.02, 0.13
最近1、5、15分钟的平均负载
负载值 <= CPU数量 * 2, 正常
查看系统负载
[root@localhost ~]# uptime
15:25:02 up 6:21, 2 users, load average: 0.01, 0.02, 0.06
查看CPU的数量
[root@localhost ~]# nproc
4
[root@localhost ~]#
[root@localhost ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
- 显示系统中的进程数量
Tasks: 127 total, 1 running, 126 sleeping, 0 stopped, 0 zombie
- 显示CPU使用率
// 按数字1,分别显示每核CPU的使用率
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
us:用户进程CPU使用率
sy: 系统进程CPU使用率
id: CPU空闲
wa:等待IO的进程CPU使用率
- 显示内存容量的使用情况
KiB Mem : 995704 total, 177652 free, 516516 used, 301536 buff/cache
KiB Swap: 2097148 total, 2096884 free, 264 used. 324644 avail Mem
cache:缓存,提升数据读性能
buffer:缓冲,提升数据写性能
查看内存
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 972M 504M 173M 13M 294M 316M
Swap: 2.0G 264K 2.0G
2、查看CPU
// 查看CPU的使用率
[root@localhost ~]# mpstat 1 5
Linux 3.10.0-1160.el7.x86_64 (localhost.localdomain) 10/21/2024 _x86_64_ (4 CPU)
02:19:01 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:19:02 PM all 0.00 0.00 43.33 5.90 0.00 2.31 0.00 0.00 0.00 48.46
02:19:03 PM all 0.25 0.00 39.95 3.82 0.00 2.80 0.00 0.00 0.00 53.18
02:19:04 PM all 0.00 0.00 41.22 3.56 0.00 2.29 0.00 0.00 0.00 52.93
02:19:05 PM all 0.00 0.00 40.92 5.37 0.00 2.05 0.00 0.00 0.00 51.66
02:19:06 PM all 0.00 0.00 39.74 5.64 0.00 2.31 0.00 0.00 0.00 52.31
Average: all 0.05 0.00 41.03 4.85 0.00 2.35 0.00 0.00 0.00 51.71
// 查看CPU型号 i/e/w系列, 龙芯/兆芯/鲲鹏/飞腾
[root@localhost ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 4
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 142
Model name: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
3、查看内存
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 972M 241M 60M 7.7M 671M 581M
Swap: 2.0G 0B 2.0G
- 查看内存的使用率
[root@localhost ~]# vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
4 0 0 517224 164 333756 0 0 37 122646 773 101 0 27 70 3 0
2 0 0 71760 164 677052 0 0 0 871427 4901 512 0 44 50 6 0
4 0 0 67960 164 680340 0 0 0 906244 5312 505 0 43 52 5 0
procs:
r 正在运行的进程
b 阻塞的进程
- 查看内存的型号
[root@localhost ~]# dmidecode memory | less
# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 2.7 present.
620 structures occupying 29188 bytes.
Table at 0x000E0010.
Handle 0x0000, DMI type 0, 24 bytes
BIOS Information
Vendor: Phoenix Technologies LTD
Vendor:厂商
3、查看硬盘
- 查看容量
[root@localhost ~]# df -hT /
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 18G 5.8G 12G 33% /
[root@localhost ~]# df -i /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/centos-root 9179136 71481 9107655 1% /
- 查看硬盘IO
[root@localhost ~]# iostat 1 1
Linux 3.10.0-1160.el7.x86_64 (localhost.localdomain) 10/21/2024 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.09 0.00 31.28 3.57 0.00 65.06
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 1158.20 128.27 589266.48 398012 1828429060
scd0 0.01 0.33 0.00 1028 0
dm-0 1158.12 124.42 589265.98 386070 1828427524
dm-1 0.03 0.71 0.00 2204 0
4、查看网卡
- 查看网卡流量/IO
[root@localhost ~]# sar -n DEV 1 3
Linux 3.10.0-1160.el7.x86_64 (localhost.localdomain) 10/21/2024 _x86_64_ (4 CPU)
03:29:54 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
03:29:55 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
03:29:55 PM ens33 1.98 1.98 0.12 0.55 0.00 0.00 0.00
rxpck/s:每秒接收的数据包量
txpck/s:每秒发送的数据包量
rxKB/s: 每秒接收的数据流量, 单位kb
txKB/s: 每秒发送的数据流量, 单位kb
rxcmp/s:每秒接收的压缩数据量
txcmp/s:每秒发送的压缩数据量
rxmcst/s:每秒接收的组播的数据量
- 查看网卡的带宽
[root@localhost ~]# ethtool ens33
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 1000Mb/s // 网卡带宽
五、stress系统压测工具
stress,系统压测工具,模拟进程操作,测试系统性能
1、安装stress
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum install stress -y
[root@localhost ~]# rpm -q stress
stress-1.0.4-16.el7.x86_64
2、测试cpu使用率
[root@localhost ~]# stress -c 2
stress: info: [2869] dispatching hogs: 2 cpu, 0 io, 0 vm, 0 hdd
3、模拟sync()将内存中的数据同步到硬盘
[root@localhost ~]# stress -i 2
stress: info: [2891] dispatching hogs: 0 cpu, 2 io, 0 vm, 0 hdd
4、测试硬盘IO
[root@localhost ~]# stress -d 1 --hdd-bytes 2G
stress: info: [2986] dispatching hogs: 0 cpu, 0 io, 0 vm, 1 hdd
# top 或 iostat
5、测试内存分配
[root@localhost ~]# stress -m 10 --vm-bytes 512M
stress: info: [3116] dispatching hogs: 0 cpu, 0 io, 10 vm, 0 hdd
stress: FAIL: [3116] (415) <-- worker 3125 got signal 9
stress: WARN: [3116] (417) now reaping child worker processes
stress: FAIL: [3116] (451) failed run completed in 8s
六、linux系统运行模式
1、运行模式
0 关机模式, init 0
1 单用户模式,重置root密码
2 字符模式, 无网络
3 完整的字符模式【常用】, multi-user.target
4 预留
5 图形模式【常用】, graphical.target
6 重启模式
2、切换运行模式
- 临时切换
// 查看运行模式
[root@martin-host ~]# runlevel
N 5
[root@martin-host ~]# init 3
[root@martin-host ~]# init 5
// 字符模式
[root@martin-host ~]# systemctl isolate multi-user.target
// 图形模式
[root@martin-host ~]# systemctl isolate graphical.target
- 系统默认的运行模式
[root@martin-host ~]# systemctl set-default multi-user.target



2001

被折叠的 条评论
为什么被折叠?



