目录
一.线程、进程、程序
二.进程的五种状态
三.ps命令查看静态的进程统计信息
四.top命令查看进程的动态信息
五.pgrep命令查询进程PID号
六.pstree查看进程树
七.启动进程的方式
八.改变进程的运行方式
九.终止进程的运行
十.at命令,一次性任务设置
十一.crontab命令,周期性任务设置
[root@localhost ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.6 128212 6836 ? Ss 11:21 0:05 /usr/lib/syst
root 2 0.0 0.0 0 0 ? S 11:21 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 11:21 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 11:21 0:00 [kworker/0:0H
root 7 0.0 0.0 0 0 ? S 11:21 0:00 [migration/0]
......(省略)
root 4533 0.0 0.1 151112 1836 pts/0 R+ 14:24 0:00 ps aux
[root@localhost ~]#
注:USER 表示启动该进程的用户账号;PID:在系统唯一的进程号; %CPU:cpu的使用率; %MEM:内存的使用率; VSZ:占用虚拟内存的大小;RSS:占用物理内存的大小; TTY:表示该进程在那个终端上运行,”?“表示未知或者不需要终端;STAT:显示当前进程的状态,如S(休眠)、R(运行)、Z(僵死)、<(高优先级) 、N(低优先级)、s(父进程)、+(前台进程);START:启动该进程的时间 ; TIME:该进程占用CPU的时间; COMMAND:启动该进程的命令的名称。
[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 - 32053 ep_pol 11:21 ? 00:00:05 /usr/lib/systemd/systemd --switched-root --system
1 S root 2 0 0 80 0 - 0 kthrea 11:21 ? 00:00:00 [kthreadd]
1 S root 3 2 0 80 0 - 0 smpboo 11:21 ? 00:00:00 [ksoftirqd/0]
1 S root 5 2 0 60 -20 - 0 worker 11:21 ? 00:00:00 [kworker/0:0H]
1 S root 7 2 0 -40 - - 0 smpboo 11:21 ? 00:00:00 [migration/0]
......(省略)
0 R root 4849 4138 0 80 0 - 37778 - 14:51 pts/0 00:00:00 ps -elf
[root@localhost ~]#
[root@localhost ~]# ps aux | grep bash
root 723 0.0 0.0 115308 952 ? S 11:21 0:00 /bin/bash /usr/sbin/ksmtuned
root 4138 0.0 0.3 116232 3048 pts/0 Ss 13:54 0:00 -bash
root 5079 0.0 0.0 112728 972 pts/0 R+ 15:08 0:00 grep --color=auto bash
[root@localhost ~]#
[root@localhost ~]# top
top - 15:14:57 up 3:53, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 144 total, 1 running, 143 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 999696 total, 375844 free, 298640 used, 325212 buff/cache
KiB Swap: 2096124 total, 2096124 free, 0 used. 487396 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 128212 6836 4076 S 0.0 0.7 0:05.75 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.44 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
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:01.29 rcu_sched
10 root rt 0 0 0 0 S 0.0 0.0 0:00.16 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.01 khungtaskd
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
25 root 20 0 0 0 0 S 0.0 0.0 0:00.22 kswapd0
26 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
27 root 39 19 0 0 0 S 0.0 0.0 0:00.37 khugepaged
28 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
36 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld
注意:在以上页面中,按C表示cpu从大到小排列,按M表示内存从大到小排列,按q退出页面或者按k再回车
[root@localhost ~]# pgrep -l "ksmd"
26 ksmd
[root@localhost ~]# pgrep -l ksmd
26 ksmd
[root@localhost ~]# pgrep ksmd
26
[root@localhost ~]#
[root@localhost ~]# pgrep -l -U root
1 systemd
2 kthreadd
3 ksoftirqd/0
5 kworker/0:0H
7 migration/0
8 rcu_bh
9 rcu_sched
10 watchdog/0
12 kdevtmpfs
......(省略)
6223 sleep
[root@localhost ~]# pgrep -l -U root -t tty1
1151 X
[root@localhost ~]# pgrep -l -U root -t pts/0
4138 bash
[root@localhost ~]#
[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─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
├─dnsmasq───dnsmasq
├─firewalld───{firewalld}
├─gdm─┬─X───{X}
│ ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───4*[{gnome-settings-}]
│ │ │ ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
│ │ │ │ │ ├─ibus-engine-sim───2*[{ibus-engine-sim}]
│ │ │ │ │ └─2*[{ibus-daemon}]
│ │ │ │ └─6*[{gnome-shell}]
│ │ │ └─3*[{gnome-session-b}]
│ │ └─2*[{gdm-session-wor}]
│ └─3*[{gdm}]
├─gssproxy───5*[{gssproxy}]
├─ibus-x11───2*[{ibus-x11}]
├─ksmtuned───sleep
├─libvirtd───15*[{libvirtd}]
├─lsmd
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─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
├─tuned───4*[{tuned}]
├─upowerd───2*[{upowerd}]
├─vmtoolsd───{vmtoolsd}
├─wpa_supplicant
└─xdg-permission-───2*[{xdg-permission-}]
[root@localhost ~]# pstree -aup
systemd,1 --switched-root --system --deserialize 21
├─ModemManager,621
│ ├─{ModemManager},625
│ └─{ModemManager},697
├─NetworkManager,726 --no-daemon
│ ├─dhclient,4020 -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens33.pid -lf...
│ ├─{NetworkManager},734
│ └─{NetworkManager},739
├─VGAuthService,643 -s
├─abrt-watch-log,617 -F BUG: WARNING: at WARNING: CPU: INFO: possible recursive locking detected ernel BUG atlist_del corr
├─abrt-watch-log,640 -F Backtrace /var/log/Xorg.0.log -- /usr/bin/abrt-dump-xorg -xD
├─abrtd,616 -d -s
├─accounts-daemon,636
│ ├─{accounts-daemon},641
│ └─{accounts-daemon},691
├─alsactl,615 -s -n 19 -c -E ALSA_CONFIG_PATH=/etc/alsa/alsactl.conf --
......(省略)
├─dbus-daemon,1306,gdm --fork --print-pid 4 --print-address 6 --session
│ └─{dbus-daemon},1307
├─dbus-daemon,650,dbus --system --address=systemd: --nofork --nopidfile --systemd-activation
│ └─{dbus-daemon},688
├─dbus-launch,1305,gdm --exit-with-session /usr/libexec/gnome-session-binary --autostart ...
├─dnsmasq,1234,nobody --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro...
│ └─dnsmasq,1237,root --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro...
├─firewalld,725 -Es /usr/sbin/firewalld --nofork --nopid
│ └─{firewalld},845
├─gdm,1066
│ ├─X,1151 :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-KsPy5j/database -seat seat0 ...
│ │ └─{X},1245
│ ├─gdm-session-wor,1269
│ │ ├─gnome-session-b,1299,gdm --autostart /usr/share/gdm/greeter/autostart
│ │ │ ├─gnome-settings-,1400
│ │ │ │ ├─{gnome-settings-},1404
│ │ │ │ ├─{gnome-settings-},1406
│ │ │ │ ├─{gnome-settings-},1407
│ │ │ │ └─{gnome-settings-},1408
│ │ │ ├─gnome-shell,1327
│ │ │ │ ├─ibus-daemon,1369 --xim --panel disable
│ │ │ │ │ ├─ibus-dconf,1373
│ │ │ │ │ │ ├─{ibus-dconf},1376
│ │ │ │ │ │ ├─{ibus-dconf},1379
│ │ │ │ │ │ └─{ibus-dconf},1380
│ │ │ │ │ ├─ibus-engine-sim,1431
│ │ │ │ │ │ ├─{ibus-engine-sim},1433
│ │ │ │ │ │ └─{ibus-engine-sim},1434
│ │ │ │ │ ├─{ibus-daemon},1371
│ │ │ │ │ └─{ibus-daemon},1375
│ │ │ │ ├─{gnome-shell},1328
│ │ │ │ ├─{gnome-shell},1330
│ │ │ │ ├─{gnome-shell},1346
│ │ │ │ ├─{gnome-shell},1361
│ │ │ │ ├─{gnome-shell},1362
│ │ │ │ └─{gnome-shell},1363
│ │ │ ├─{gnome-session-b},1322
│ │ │ ├─{gnome-session-b},1323
│ │ │ └─{gnome-session-b},1325
│ │ ├─{gdm-session-wor},1272
│ │ └─{gdm-session-wor},1273
│ ├─{gdm},1072
│ ├─{gdm},1073
│ └─{gdm},1150
├─wpa_supplicant,1396 -u -f /var/log/wpa_supplicant.log -c /etc/wpa_supplicant/wpa_supplicant.conf -P/var/run/wpa_supplica
└─xdg-permission-,1385,gdm
├─{xdg-permission-},1387
└─{xdg-permission-},1392
[root@localhost ~]#
[root@localhost ~]# pstree -ap root
systemd,1 --switched-root --system --deserialize 21
├─ModemManager,621
│ ├─{ModemManager},625
│ └─{ModemManager},697
├─NetworkManager,726 --no-daemon
│ ├─dhclient,4020 -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens33.pid -lf...
│ ├─{NetworkManager},734
│ └─{NetworkManager},739
├─VGAuthService,643 -s
├─abrt-watch-log,617 -F BUG: WARNING: at WARNING: CPU: INFO: possible recursive locking detected ernel BUG atlist_del corr
├─abrt-watch-log,640 -F Backtrace /var/log/Xorg.0.log -- /usr/bin/abrt-dump-xorg -xD
├─abrtd,616 -d -s
......(省略)
/etc/wpa_supplicant/wpa_supplicant.conf -P/var/run/wpa_supplica
└─xdg-permission-,1385
├─{xdg-permission-},1387
└─{xdg-permission-},1392
[root@localhost ~]#
[root@localhost ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# du -sh /mnt/Packages
3.8G /mnt/Packages
[root@localhost ~]# cp -r /mnt/Packages /opt &
[1] 6731
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.43.133 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::fe27:a784:dd8d:8487 prefixlen 64 scopeid 0x20
ether 00:0c:29:de:a0:aa txqueuelen 1000 (Ethernet)
RX packets 32275 bytes 30919766 (29.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14491 bytes 4676698 (4.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 68 bytes 5916 (5.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 5916 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:e8:9c:ee txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
[root@localhost ~]# ls /opt
rh
[root@localhost ~]# cp -r /mnt/Packages /opt
^Z
[1]+ 已停止 cp -i -r /mnt/Packages /opt
[root@localhost ~]# jobs
[1]+ 已停止 cp -i -r /mnt/Packages /opt
[root@localhost ~]# jobs -l
[1]+ 7672 停止 cp -i -r /mnt/Packages /opt
[root@localhost ~]#
[root@localhost ~]# jobs
[1]+ 已停止 cp -i -r /mnt/Packages /opt
[root@localhost ~]# bg 1
[1]+ cp -i -r /mnt/Packages /opt &
[root@localhost ~]# jobs 1
[1]+ 运行中 cp -i -r /mnt/Packages /opt &
[root@localhost ~]# rm -rf /opt/Packages
[root@localhost ~]# cp -r /mnt/Packages /opt
^Z
[1]+ 已停止 cp -i -r /mnt/Packages /opt
[root@localhost ~]# jobs
[1]+ 已停止 cp -i -r /mnt/Packages /opt
[root@localhost ~]# fg 1
cp -i -r /mnt/Packages /opt
^Z
[1]+ 已停止 cp -i -r /mnt/Packages /opt
[root@localhost ~]#
[root@localhost opt]# pgrep -l sshd
1042 sshd
4127 sshd
[root@localhost opt]# kill 1042
[root@localhost opt]# pgrep -l sshd
4127 sshd
[root@localhost opt]#
[root@localhost ~]# kill -9 7672
[root@localhost ~]# jobs -l
[1]+ 7672 已杀死 cp -i -r /mnt/Packages /opt
[root@localhost ~]# bg 1
-bash: bg: 1: 无此任务
[root@localhost opt]# pgrep -l sshd
4127 sshd
8073 sshd
[root@localhost opt]# killall sshd
at命令,设置的计划任务只在指定的任务时间执行一次,并且对应的系统服务atd必须已经运行
此外,计划执行的任务的时间、日期必须安排在当前系统的时刻之后,否则无法正确设置计划任务
在at命令行中依次指定计划执行任务的时间、日期作为参数,在"at>"提示符的任务编辑界面,每行设置一条执行命令,可以依次设置多条语句,最后按ctrl+D提交任务即可。所设置的命令操作将在计划的时间点被一次执行
以下操作,先通过date确认当前的系统时间,并设置在该时间点自动执行任务,且统计该时间点系统由root用户运行的进程数量,并且将该数值保存到指定目录文件中
实例如下:
[root@localhost ~]# date
2019年 11月 12日 星期二 19:24:24 CST
[root@localhost ~]# at 19:30 2019-11-12
at> pgrep -l -U root | wc -l > /opt/day01.txt
at>
job 1 at Tue Nov 12 19:30:00 2019
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
day01.txt rh
[root@localhost opt]# cat day01.txt
172
注:任务编号是固定的
crontab的配置文件和目录:
[root@192 ~]# cat /etc/crontab
SHELL=/bin/bash //设置执行计划任务的shell环境
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
[root@192 ~]#
[root@192 ~]# ls /etc/cron.*/
/etc/cron.d/:
0hourly raid-check sysstat
/etc/cron.daily/: //该目录下,存放系统每天要做的任务脚本
logrotate man-db.cron mlocate
/etc/cron.hourly/: //该目录下,存放系统每小时要做的任务脚本
0anacron
/etc/cron.monthly/: //该目录下,存放系统每个月要做的任务脚本
/etc/cron.weekly/: //该目录下,存放系统每周要做的任务脚本
[root@192 ~]#
注:crond守护进程会自动检查/etc/crontab文件、/etc/cron.d目录及/var/spool/cron/目录中的改变,若有配置改变,它们就会被载入内存,即不需要重启,crond守护进程就可以使设置生效
[root@192 ~]# crontab -e //相当于vim编辑器,且操作相同
no crontab for root - using an empty one
crontab: installing new crontab
[root@192 ~]# crontab -l //在-e编辑里面写了这条命令,表示每三分统计
//该系统产生的进程数并且放到/opt/night.txt
*/3 * * * * ps aux | wc -l > /opt/night.txt
[root@192 ~]# crontab -r
[root@192 ~]# crontab -l
no crontab for root
[root@192 ~]# crontab -e
no crontab for root - using an empty one
crontab: no changes made to crontab
[root@192 ~]# cd /opt
[root@192 opt]# ls
day01.txt day02.txt night.txt rh
[root@192 opt]# cat night.txt
145
[root@192 opt]#
50 7 * * * /bin/systemctl sshd start 每天7:50开启ssh服务
50 22 * * * /sbin/systemctl sshd stop 每天22:50关闭ssh服务
0 0 */5 * * /bin/rm -rf /var/ftp/pub/* 每隔5天清空该目录中的数据
30 7 * * 6 /sbin/systemctl httpd restart 每周六7:30重启httpd服务
30 17 * * 1,3,5 /bin/tar cfvj httpdconf.tar.bz2 /etc/httpd/ 周1,3,5,17:30就压缩该目录下的数据