Linux作业控制

 

 

 

启动、停止、无条件终止以及恢复作业的这些功能统称为作业控制,通过作业控制,你就能完全控制shell中正在运行的所有作业。

 

标注:Ctrl+C组合键结束正在运行的作业,Ctrl+Z组合键暂停正在运行的作业。

 

 

 

1、查看作业

 

jobs命令允许查看shell当前正在处理的作业:

 [root@cloucentos6 home]# ping 192.168.10.10

PING 192.168.10.10(192.168.10.10) 56(84) bytes of data.

64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=2.92 ms

64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.555 ms

^Z                                                                                                    #Ctrl+Z组合键暂停作业

[1]+  Stopped                 ping 192.168.10.10

[root@cloucentos6 home]# jobs  -l

[1]+  4227 停止                  ping 192.168.10.10

 

标注:jobs命令显示作业时,会看见带加号的作业会被当作默认的作业,带减号的作业是后面要执行的作业,如何多个作业一般只有一个加号和一个减号,默认作业执行加号后再执行减号的作业。

[root@cloucentos6 home]# jobs  -l

[1]   4227 停止                  ping 192.168.10.10

[2]   4241 停止                  ./test.sh

[3]-  4244 停止                  ./test.sh

[4]+  4246 停止                  ./test.sh

 

 

 

 

2、重启暂停的作业

 

在bash作业控制中,可以将已暂停的作业作为后台进程或前台进程重启。

(1)、以后台模式重启一个作业,可以使用bg命令加上作业号:

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

ping -c 5 192.168.10.254 > /home/ifconfig.txt

[root@cloucentos6 home]# ./test.sh

^Z

[1]+  Stopped                 ./test.sh

[root@cloucentos6 home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.633 ms

[root@cloucentos6 home]# bg  1

[1]+ ./test.sh &

[root@cloucentos6 home]# jobs

[1]+  Done                    ./test.sh

[root@cloucentos6 home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.633 ms

64 bytes from 192.168.10.254: icmp_seq=2 ttl=125 time=17.8 ms

64 bytes from 192.168.10.254: icmp_seq=3 ttl=125 time=0.469 ms

64 bytes from 192.168.10.254: icmp_seq=4 ttl=125 time=0.713 ms

64 bytes from 192.168.10.254: icmp_seq=5 ttl=125 time=0.536 ms

 

--- 192.168.10.254 ping statistics ---

5 packets transmitted, 5 received, 0% packet loss, time 15229ms

rtt min/avg/max/mdev = 0.469/4.030/17.802/6.886 ms

 

 

(2)、以后台模式重启一个作业,可以使用bg命令加上作业号:

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

ping -c 5 192.168.10.254 > /home/ifconfig.txt

[root@cloucentos6 home]# ./test.sh

^Z

[1]+  Stopped                 ./test.sh

[root@cloucentos6 home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.573 ms

[root@cloucentos6 home]# fg   1

./test.sh

[root@cloucentos6 home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.573 ms

64 bytes from 192.168.10.254: icmp_seq=2 ttl=125 time=1.89 ms

64 bytes from 192.168.10.254: icmp_seq=3 ttl=125 time=1.87 ms

64 bytes from 192.168.10.254: icmp_seq=4 ttl=125 time=2.20 ms

64 bytes from 192.168.10.254: icmp_seq=5 ttl=125 time=0.773 ms

 

--- 192.168.10.254 ping statistics ---

5 packets transmitted, 5 received, 0% packet loss, time 25642ms

rtt min/avg/max/mdev = 0.573/1.462/2.204/0.659 ms

 

 

 

 

3、结束后台运行或暂停的作业

 

使用kill命令即可杀死进程,需要注意如果作业有子进程和父进程的,如果直接结束作业父进程,子进程还是会继续运行,所以,需要先结束子进程再结束父进程,ps命令可以查看作业的子进程和父进程,jobs只能看到父进程。

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

ping -c 5 10.98.97.1 > /home/ifconfig.txt

[root@cloucentos6 home]# cat ifconfig.txt

PING 10.98.97.1 (10.98.97.1) 56(84) bytes of data.

64 bytes from 10.98.97.1: icmp_seq=1 ttl=125 time=0.565 ms

[root@cloucentos6 home]# ./test.sh

^Z

[1]+  Stopped                 ./test.sh

[root@cloucentos6 home]# jobs  -l

[1]+  4346 停止                  ./test.sh                         #父进程

[root@cloucentos6 home]# ps au | grep T

USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root       4346  0.0  0.0 106008  1144 pts/0    T    23:28   0:00 /bin/bash ./test.sh                #父进程

root       4347  0.0  0.0 103164   676 pts/0    T    23:28   0:00 ping -c 5 10.98.97.1             #子进程

root       4350  0.0  0.0 103160   816 pts/0    S+   23:29   0:00 grep T

[root@cloucentos6 home]# kill -9 4347                         #先结束子进程

[root@cloucentos6 home]# kill -9 4346                         #最后结束父进程

[root@cloucentos6 home]# jobs

[1]+  已杀死               ./test.sh

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值