进程管理
(1)进程查看
使用ps
命令查看进程:
ps
:表示查看和当前用户有关的进程。
# 使用"ps"命令查看和当前用户有关的进程
fu@fu-virtual-machine:~$ ps
PID TTY TIME CMD
2731 pts/0 00:00:00 bash
3100 pts/0 00:00:00 ps
ps -l
:查看和当前用户有关进程的详细信息。
# 使用"ps -l"命令查看和当前用户有关进程的详细信息
fu@fu-virtual-machine:~$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 2731 2720 0 80 0 - 4845 do_wai pts/0 00:00:00 bash
0 R 1000 3104 2731 0 80 0 - 5017 - pts/0 00:00:00 ps
ps -el
:查看所有运行进程的详细信息,-e
与-A
参数作用一样,都是查看全部进程。
# 查看系统中所有运行中进程的详细信息
fu@fu-virtual-machine:~$ ps -el
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 1 0 0 80 0 - 25510 - ? 00:00:05 systemd
1 S 0 2 0 0 80 0 - 0 - ? 00:00:00 kthreadd
1 I 0 3 2 0 60 -20 - 0 - ? 00:00:00 rcu_gp
1 I 0 4 2 0 60 -20 - 0 - ? 00:00:00 rcu_par_gp
1 I 0 6 2 0 60 -20 - 0 - ? 00:00:00 kworker/0:
1 I 0 9 2 0 60 -20 - 0 - ? 00:00:00 mm_percpu_
# ...此后还有很多运行中进程
进程详细信息中有PID
和PPID
两列,分别表示当前进程的编号
和启动该进程的父进程编号
。
使用进程树命令pstree
查看进程:
pstree
:以树的组织形式打印系统中运行的所有进程。
# 使用"pstree"命令查看进程树
fu@fu-virtual-machine:~$ pstree
systemd─┬─ModemManager───2*[{
ModemManager}]
├─NetworkManager───2*[{
NetworkManager}]
├─VGAuthService
├─accounts-daemon───2*[{
accounts-daemon}]
├─acpid
├─avahi-daemon───avahi-daemon
├─bluetoothd
├─colord───2*[{
colord}]
# ...此后还有很多显示
pstree -p
:在pstree
的基础上显示每个进程的PID
。
# 查看进程树,带"PID"
fu@fu-virtual-machine:~$ pstree -p
systemd(1)─┬─ModemManager(828)─┬─{
ModemManager}(847)
│ └─{
ModemManager}(851)
├─NetworkManager(738)─┬─{
NetworkManager}(797)
│ └─{
NetworkManager}(808)
├─VGAuthService(701)
├─accounts-daemon(723)─┬─{
accounts-daemon}(733)
│ └─{
accounts-daemon}(804)
# ...此后还有很多显示
使用top
命令动态监控进程:
top
:动态监控所有进程,使用该命令后,会进入top
环境,在该环境中,h
表示查看帮助;N
表示进程以PID
排序显示;P
表示进程以按CPU占有率排序显示;M
表示进程按内存资源占有率排序显示;T
表示进程按使用CPU累计时间排序显示;q
表示退出;(注意大小写
)
top -p 1000
:动态监控PID
为1000
的进程状态。
(2)杀死进程:
杀死进程的命令为kill
kill -9 3169
:杀死PID
为3169
的进程。
# 查看和当前用户有关的进程
fu@fu-virtual-machine:~$ ps
PID TTY TIME CMD
2731 pts/0 00:00:00 bash
3169 pts/0 00:00:00 nano
3170 pts/0 00:00:00 ps
# 使用"kill"命令杀死进程编号为"3169"的进程
fu@fu-virtual-machine:~$ kill -9 3169
# 成功杀死进程
fu@fu-virtual-machine:~$ ps
PID TTY TIME CMD
2731 pts/0 00:00:00 bash
3171 pts/0 00:00:00 ps
[1]+ 已杀死 nano
循环任务管理
循环任务管理命令为:crontab
。
crontab -e
:创建循环任务;如果第一次执行该命令首先会让用户指定用什么编辑器去编辑/var/spool/cron/crontabs
目录下的循环任务文件,这个任务文件名为用户名。
循环任务文件如下:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
创建循环任务的语法为:分 时 日 月 周 命令
,周
的取值为1-7
,即星期一到星期二。
在以上显示的表示时间的字段可以使用如下符号:
*:代表所有可能的值,例如月份字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
,:可以用逗号隔开的值指定一个列表范围,例如,"1,2,5,7,8,9"。
-:可以用整数之间的中杠表示一个整数范围,例如"2-6"表示"2,3,4,5,6"。
/:可以用"/"指定时间的间隔频率,"/"可以和"*"一起使用,例如"*/10",如果用在分钟字段,表示每十分钟执行一次。
tty
:该命令打印出当前用户bash
的终端设备名。
fu@fu-virtual-machine:/var/spool/cron$ tty
/dev/pts/0
30-50/5 12-13 10 12 * echo Hello! >> /dev/pts/0
:表示每年的12
月10
日的12:30-12:50
和13:30-13:50
的时间段,每隔5
分钟向bash
输出Hello!
。
crontab -l
:查看任务文件。
fu@fu-virtual-machine:/var/spool/cron$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0-30/5 15-16 * * * echo "Auto print Hello!" >> /dev/pts/0
crontab -r
:清除任务。
# 查看任务文件的最后一行
fu@fu-virtual-machine:/var/spool/cron$ sudo tail -n 1 /var/spool/cron/crontabs/fu
[sudo] fu 的密码:
0-30/5 15-16 * * * echo "Auto print Hello!" >> /dev/pts/0
# 清除任务
fu@fu-virtual-machine:/var/spool/cron$ crontab -r
# 清除成功
fu@fu-virtual-machine:/var/spool/cron$ crontab -l
no crontab for fu
系统服务
系统服务的命令为:systemctl
systemctl status crontab
:查看cron
服务的状态。
# 查看"cron"服务的状态
fu@fu-virtual-machine:/var/spool/cron$ systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: >
Active: active (running) since Sat 2021-10-30 16:37:46 CST; 23h ago
Docs: man:cron(8)
Main PID: 734 (cron)
Tasks: 1 (limit: 2196)
Memory: 560.0K
CGroup: /system.slice/cron.service
└─734 /usr/sbin/cron -f
10月 31 16:25:01 fu-virtual-machine CRON[3736]: pam_unix(cron:session): session>
10月 31 16:25:01 fu-virtual-machine CRON[3738]: (fu) CMD (echo "Auto print Hell>
10月 31 16:25:01 fu-virtual-machine CRON[3736]: pam_unix(cron:session): session>
10月 31 16:30:01 fu-virtual-machine CRON[3791]: pam_unix(cron:session): session>
10月 31 16:30:01 fu-virtual-machine CRON[3794]: (root) CMD ([ -x /etc/init.d/an>
10月 31 16:30:01 fu-virtual-machine CRON[3791]: pam_unix(cron:session): session>
10月 31 16:36:01 fu-virtual-machine cron[734]: (fu) RELOAD (crontabs/fu)
10月 31 16:36:01 fu-virtual-machine CRON[3833]: pam_unix(cron:session): session>
10月 31 16:36:01 fu-virtual-machine CRON[3834]: (fu) CMD (echo "Hello" > /dev/p>
10月 31 16:36:01 fu-virtual-machine CRON[3833]: pam_unix(cron:session): session>
lines 1-20/20 (END)
systemctl stop cron
:停止cron
服务。
# 停止"cron"服务
fu@fu-virtual-machine:~$ systemctl stop cron
# 查看"cron"服务的状态
fu@fu-virtual-machine:~$ systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: >
Active: inactive (dead) since Sun 2021-10-31 16:38:56 CST; 14s ago
Docs: man:cron(8)
Process: 734 ExecStart=/usr/sbin/cron -f $EXTRA_OPTS (code=killed, signal=T>
Main PID: 734 (code=killed, signal=TERM)
10月 31 16:36:01 fu-virtual-machine CRON[3833]: pam_unix(cron:session): session>
10月 31 16:37:01 fu-virtual-machine CRON[3842]: pam_unix(cron:session): session>
10月 31 16:37:01 fu-virtual-machine CRON[3843]: (fu) CMD (echo "Hello" > /dev/p>
10月 31 16:37:01 fu-virtual-machine CRON[3842]: pam_unix(cron:session): session>
10月 31 16:38:01 fu-virtual-machine CRON[3844]: pam_unix(cron:session): session>
10月 31 16:38:01 fu-virtual-machine CRON[3845]: (fu) CMD (echo "Hello" > /dev/p>
10月 31 16:38:01 fu-virtual-machine CRON[3844]: pam_unix(cron:session): session>
10月 31 16:38:56 fu-virtual-machine systemd[1]: Stopping Regular background pro>
10月 31 16:38:56 fu-virtual-machine systemd[1]: cron.service: Succeeded.
# "Stopped"表示该服务被停止了
10月 31 16:38:56 fu-virtual-machine systemd[1]: Stopped Regular background prog>
lines 1-17/17 (END)
systemctl start cron
:表示启动cron
服务。
# 启动"cron"服务
fu@fu-virtual-machine:~$ systemctl start cron
# 查看"cron"服务的状态
fu@fu-virtual-machine:~$ systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: >
Active: active (running) since Sun 2021-10-31 16:41:42 CST; 6s ago
Docs: man:cron(8)
Main PID: 3880 (cron)
Tasks: 1 (limit: 2196)
Memory: 360.0K
CGroup: /system.slice/cron.service
└─3880 /usr/sbin/cron -f
# "Started"表示启动了服务
10月 31 16:41:42 fu-virtual-machine systemd[1]: Started Regular background prog>
10月 31 16:41:42 fu-virtual-machine cron[3880]: (CRON) INFO (pidfile fd = 3)
10月 31 16:41:42 fu-virtual-machine cron[3880]: (CRON) INFO (Skipping @reboot j>
lines 1-13/13 (END)
systemctl restart cron
:表示重新启动cron
服务。
# 查看"cron"服务的状态,现在时停止状态
fu@fu-virtual-machine:~$ systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: >
Active: inactive (dead) since Sun 2021-10-31 16:44:40 CST; 8s ago
Docs: man:cron(8)
Process: 3913 ExecStart=/usr/sbin/cron -f $EXTRA_OPTS (code=killed, signal=>
Main PID: 3913 (code=killed, signal=TERM)
10月 31 16:44:20 fu-virtual-machine systemd[1]: Started Regular background prog>
10月 31 16:44:20 fu-virtual-machine cron[3913]: (CRON) INFO (pidfile fd = 3)
10月 31 16:44:20 fu-virtual-machine cron[3913]: (CRON) INFO (Skipping @reboot j>
10月 31 16:44:40 fu-virtual-machine systemd[1]: Stopping Regular background pro>
10月 31 16:44:40 fu-virtual-machine systemd[1]: cron.service: Succeeded.
# 停止
10月 31 16:44:40 fu-virtual-machine systemd[1]: Stopped Regular background prog>
# 重启"cron"服务
fu@fu-virtual-machine:~$ systemctl restart cron
# 查看"cron"服务的状态
fu@fu-virtual-machine:~$ systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: >
Active: active (running) since Sun 2021-10-31 16:45:03 CST; 7s ago
Docs: man:cron(8)
Main PID: 3937 (cron)
Tasks: 1 (limit: 2196)
Memory: 360.0K
CGroup: /system.slice/cron.service
└─3937 /usr/sbin/cron -f
# "Started",重启成功
10月 31 16:45:03 fu-virtual-machine systemd[1]: Started Regular background prog>
10月 31 16:45:04 fu-virtual-machine cron[3937]: (CRON) INFO (pidfile fd = 3)
10月 31 16:45:04 fu-virtual-machine cron[3937]: (CRON) INFO (Skipping @reboot j>
systemctl disable cron
:禁用cron
服务。
systemctl enable cron
:使cron
服务可用。
systemctl list-units
:列出正在运行的服务
# 列出正在运行的服务
fu@fu-virtual-machine:~$ systemctl list-units | grep cron
cron.service loaded active running Regular background program processing daemon
anacron.timer loaded active waiting Trigger anacron every hour