玩转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@servera ~]$ 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 19:51:21 2023 from 172.16.190.227
[student@servera ~]$

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

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

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

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

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

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

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

3 执行control脚本。该脚本以一秒为间隔, 不断将词语“technical”和一个空格附加到文件~/control_outfile中。

[student@servera ~]$ control technical

4 在右终端shell中, 使用tail命令及-f选项确认新进程正在写入
/home/student/control_outfile文件。

[student@servera ~]$ tail -f ~/control_outfile 
technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical ^C
[student@servera ~]$

5 在左终端shell中, 按Ctrl+z来暂停正在运行的进程。shell将返回作业ID并用方括号括起。在右窗口中,确认进程输出已经停止。

[student@servera ~]$ control technical
^Z
[1]+  Stopped                 control technical
[student@servera ~]$
[student@servera ~]$ tail -f ~/control_outfile 
technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical

6 在左终端shell中, 查看jobs列表。记住+符号表示默认作业。在后台重新启动该作业。在右终端shell中, 确认进程输出再次处于活动状态。

6.1 使用jobs命令, 查看作业列表。

[student@servera ~]$ jobs
[1]+  Stopped                 control technical
[student@servera ~]$

6.2 使用bg命令, 在后台重新启动control作业。

[student@servera ~]$ bg
[1]+ control technical &
[student@servera ~]$

6.3 使用jobs命令, 确认control作业再次处于运行状态。

[student@servera ~]$ jobs
[1]+  Running                 control technical &
[student@servera ~]$

6.4 在右终端shell中, 确认tail命令正在生成输出。

[student@servera ~]$ tail -f ~/control_outfile 
technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical

7 在左终端shell中, 再启动两个control进程, 以便向~/output文件附加内容。使用与符号(&)在后台启动进程。将technical替换为documents, 然后替换为database。替换参数有助于区分这三个进程。

[student@servera ~]$ control documents &
[2] 12996
[student@servera ~]$ control database &
[3] 13028
[student@servera ~]$

8 在左终端shell中, 使用jobs命令查看正在运行的三个进程。在右终端shell中, 确认所有三个进程都附加内容到文件中。

[student@servera ~]$ jobs
[1]   Running                 control technical &
[2]-  Running                 control documents &
[3]+  Running                 control database &
[student@servera ~]$
[student@servera ~]$ tail -f ~/control_outfile 
technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical documents technical documents technical documents technical documents technical documents technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical

9 暂停control technical进程。确认它已被暂停。终止control documents进程,并确认它已被终止。

9.1 在左终端shell中, 使用fg命令及作业ID将control technical进程转到前台。按Ctrl+z暂停进程。使用jobs命令确认进程已被暂停。

[student@servera ~]$ fg %1
control technical
^Z
[1]+  Stopped                 control technical
[student@servera ~]$ jobs
[1]+  Stopped                 control technical
[2]   Running                 control documents &
[3]-  Running                 control database &
[student@servera ~]$

9.2 在右终端shell中, 确认control technical进程不再发送输出。

[student@servera ~]$ tail -f ~/control_outfile 
technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical documents technical documents technical documents technical documents technical documents technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents

9.3 在左终端shell中, 使用fg命令及作业ID将control documents进程转到前台。按Ctrl+c终止进程。使用jobs命令确认进程已被终止。

[student@servera ~]$ fg %2
control documents
^C
[student@servera ~]$ jobs
[1]+  Stopped                 control technical
[3]-  Running                 control database &
[student@servera ~]$

9.4 在右终端shell中, 确认control documents进程不再发送输出。

[student@servera ~]$ tail -f ~/control_outfile 
technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical technical documents technical documents technical documents technical documents technical documents technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database technical documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database documents database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database database

10 在左窗口中,使用ps命令及jT选项查看剩余的作业。已暂停作业的状态为T。其他后台作业为睡眠状态(S)。

[student@servera ~]$ ps jT
   PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
  12147   12150   12150   12150 pts/6      14654 Ss    1000   0:00 -bash
  12150   12248   12248   12150 pts/6      14654 T     1000   0:00 /bin/bash /home/student/bin/control technical
  12150   13028   13028   12150 pts/6      14654 S     1000   0:00 /bin/bash /home/student/bin/control database
  12248   13845   12248   12150 pts/6      14654 T     1000   0:00 sleep 1
  13028   14650   13028   12150 pts/6      14654 S     1000   0:00 sleep 1
  12150   14654   14654   12150 pts/6      14654 R+    1000   0:00 ps jT
[student@servera ~]$

11 在左窗口中, 使用jobs命令查看当前作业。终止control database进程, 并确认它已被终止。

[student@servera ~]$ jobs
[1]+  Stopped                 control technical
[3]-  Running                 control database &
[student@servera ~]$

使用fg命令及作业ID将control database进程转到前台。按Ctrl+c终止进程。使用jobs命令确认进程已被终止。

[student@servera ~]$ fg %3
control database
^C
[student@servera ~]$ jobs
[1]+  Stopped                 control technical
[student@servera ~]$

12 在右终端shell中, 使用Ctrl+c命令来停止tail命令。使用rm命令来删除~/control_outfile文件。

[student@servera ~]$ rm ~/control_outfile 
[student@servera ~]$

13 在两个终端中从servera注销。

[student@servera ~]$ exit
logout
Connection to servera closed.
[student@servera ~]$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值