zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    jquery----->helloworld
    hibernate------->第一个程序
    spring使用jdbcTemplate和jdbcdaosupport和namedparameter
    spring--------------->AOP
    spring------>Helloworld
    JS全选
    表单重复提交
    session
    cookies
    83. Remove Duplicates from Sorted List
  • 原文地址:https://www.cnblogs.com/zoulongbin/p/7126750.html
Copyright © 2011-2022 走看看