玩转Redhat Linux 8.0系列 | 中断进程

素材来源:Redhat Linux 8.0培训教材《RH124》、《RH134》和《RH294》

玩了5-6年的Linux,现在再来温习一遍RHCE培训教材,按照指导完成实验并与大家分享。

附上汇总贴:玩转Redhat Linux 8.0系列 | 合集_热爱编程的通信人的博客-CSDN博客


1 在workstation上, 打开两个终端窗口并将它们并排显示。在本节中, 这些终端指代为左和右。在每个终端中, 使用ssh命令以student用户身份登录servera。

[student@workstation ~]$ ssh student@servera
student@servera's password: 
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last login: Sun May 21 20:14:55 2023 from 172.16.190.227
[student@servera ~]$

2 在左窗口中,创建一个名为/home/student/bin的新目录。在新目录中,创建一个名为killing的shell脚本。使脚本成为可执行文件。

2.1 使用mkdir命令, 创建一个名为/home/student/bin的新目录。

[student@servera ~]$ mkdir /home/student/bin
[student@servera ~]$

2.2 使用vim命令, 在/home/student/bin目录中创建一个名为killing的脚本。按i 键, 以进入Vim交互模式。使用:wq命令保存文件。

[student@servera ~]$ vim /home/student/bin/killing
#!/bin/bash
while true; do
  echo -n "$@ " >> ~/killing_outfile
  sleep 5
done

2.3 使用chmod命令, 使killing文件变为可执行文件。

[student@servera ~]$ chmod +x /home/student/bin/killing 
[student@servera ~]$

3 在左终端shell中, 使用cd命令更改到/home/student/bin/目录。分别使用network、interface和connection参数, 启动三个killing进程。启动三个进程,分别名为network、interface和connection。使用与符号(&)在后台启动进程。

[student@servera ~]$ cd /home/student/bin
[student@servera bin]$ killing network &
[1] 16211
[student@servera bin]$ killing interface &
[2] 16262
[student@servera bin]$ killing connection &
[3] 16307
[student@servera bin]$

您的进程具有不同的PID编号。

4 在右终端shell中, 使用tail命令及-f选项确认所有三个进程都附加内容到
/home/student/killing_outfile文件中。

[student@servera ~]$ tail -f ~/killing_outfile 
network network interface network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection ^C
[student@servera ~]$

5 在左终端shell中, 使用jobs命令列出作业。

[student@servera bin]$ jobs
[1]   Running                 killing network &
[2]-  Running                 killing interface &
[3]+  Running                 killing connection &
[student@servera bin]$

6 使用信号暂停network进程。确认network进程状态为Stopped。在右终端shell中, 确认network进程不再附加输出到~/killing_output。

6.1 使用kill及-SIGSTOP选项来停止network进程。运行jobs以确认它已被停止。

[student@servera bin]$ kill -SIGSTOP %1
[student@servera bin]$ jobs
[1]+  Stopped                 killing network
[2]   Running                 killing interface &
[3]-  Running                 killing connection &
[student@servera bin]$

6.2 在右终端shell中, 查看来自tail命令的输出。确认词语network不再附加到~/killing_outfile文件。

[student@servera ~]$ tail -f ~/killing_outfile 
network network interface network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection ^C
[student@servera ~]$

7 在左终端shell中, 使用信号终止interface进程。确认interface进程已经消失。在右终端shell中, 确认interface进程的输出不再附加到~/killing_outfile文件。

7.1 使用kill命令及-SIGTERM选项来终止interface进程。运行jobs命令确认它已被终止。

[student@servera bin]$ kill -SIGTERM %2
[student@servera bin]$ jobs
[1]+  Stopped                 killing network
[2]   Terminated              killing interface
[3]-  Running                 killing connection &
[student@servera bin]$

7.2 在右终端shell中, 查看来自tail命令的输出。确认词语interface不再附加到~/killing_outfile文件。

[student@servera ~]$ tail -f ~/killing_outfile 
network network interface network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection connection connection connection connection connection connection ^C
[student@servera ~]$

8 在左终端shell中, 使用信号恢复network进程。确认network进程状态为Running。在右侧窗口中, 确认network进程输出正在附加到~/killing_outfile文件。

8.1 使用kill命令及-SIGCONT选项来恢复network进程。使用jobs命令确认该进程的状态为Running。

[student@servera bin]$ kill -SIGCONT %1
[student@servera bin]$ jobs
[1]+  Running                 killing network &
[3]-  Running                 killing connection &
[student@servera bin]$

8.2 在右终端shell中, 查看来自tail命令的输出。确认词语network正在附加到~/killing_outfile文件。

[student@servera ~]$ tail -f ~/killing_outfile 
network network interface network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection connection connection connection connection connection connection connection connection connection connection connection connection connection connection connection connection network connection network connection network connection network connection network connection network connection network ^C
[student@servera ~]$

9 在左终端shell中, 终止剩余的两个作业。确认没有剩余的作业, 输出也已停止。

9.1 使用kill命令及-SIGTERM选项来终止network进程。使用同样的命令终止connection进程。

[student@servera bin]$ kill -SIGTERM %1
[student@servera bin]$ kill -SIGTERM %3
[1]+  Terminated              killing network
[student@servera bin]$ jobs
[3]+  Terminated              killing connection
[student@servera bin]$

10 在左终端shell中, 列出在所有打开的终端shell中运行的tail进程。终止正在运行的tail命令。确认该进程不再运行。

10.1 使用ps命令及-ef选项来列出所有运行中的tail进程。使用grep命令来优化搜索。

[student@servera bin]$ ps -ef | grep tail
student    17050   16131  0 22:28 pts/2    00:00:00 tail -f /home/student/killing_outfile
student    17052   16085  0 22:28 pts/1    00:00:00 grep --color=auto tail
[student@servera bin]$

10.2 使用pkill命令及-SIGTERM选项来中断tail进程。使用ps确认它已不再存在。

[student@servera bin]$ pkill -SIGTERM tail
[student@servera bin]$ 
[student@servera bin]$ ps -ef | grep tail
student    17064   16085  0 22:29 pts/1    00:00:00 grep --color=auto tail
[student@servera bin]$

10.3 在右终端shell中, 确认tail命令不再运行。

[student@servera ~]$ tail -f ~/killing_outfile 
network network interface network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection network interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection interface connection connection connection connection connection connection connection connection connection connection connection connection connection connection connection connection connection network connection network connection network connection network connection network connection network connection network connection network connection network connection network connection network connection network connection network connection network connection network connection network connection Terminated
[student@servera ~]$

11 从两个终端窗口退出。不从所有会话退出将导致完成脚本运行失败。

[student@servera bin]$ exit
logout
Connection to servera closed.
[student@workstation ~]$

你可能感兴趣的:(linux,运维,服务器)