linux 工作管理

使job在后台运行:

  • 在运行指令的时候使用&符号:
    ping -c 100 www.google.com 2>&1 ping.txt &,使用数据流重定向可以避免:在后台运行的job的stdout或stderr输出到当前屏幕中,影响当前工作

    root@DESKTOP:/mnt/# ping -c 100 www.google.com 2>&1 ping.txt &
    [1] 6751
    root@DESKTOP:/mnt/# jobs -l
    [1]+  6751 Running                 ping -c 100 www.google.com ping.txt 2>&1 &
    
  • 使用ctrl+z将当前job打入后台中暂停:
    如当前job为ping -c 100 www.qq.com 使用ctrl+z将当前job打入后台

    root@DESKTOP:/mnt/# ping -c 100 www.qq.com
    PING public.sparta.mig.tencent-cloud.net (183.192.170.139) 56(84) bytes of data.
    64 bytes from . (183.192.170.139): icmp_seq=1 ttl=55 time=14.2 ms
    64 bytes from . (183.192.170.139): icmp_seq=2 ttl=55 time=14.7 ms
    64 bytes from . (183.192.170.139): icmp_seq=3 ttl=55 time=13.9 ms
    ^Z
    [1]+  Stopped                 ping -c 100 www.qq.com
    

查看job:jobs命令

  • jobs -l:列出背景中的所有job,包括job number和job的PID

    root@DESKTOP:/mnt/# jobs -l
    [1]-  6755 Stopped                 ping -c 100 www.qq.com
    [2]+  6765 Stopped                 vim test.txt
    [3]   6767 Running                 ping -c 100 www.google.com ping.txt 2>&1 &
    
  • jobs -r:列出背景中在运行的job

    root@DESKTOP:/mnt/# jobs -r
    [3]   Running                 ping -c 100 www.google.com ping.txt 2>&1 &
    
  • jobs -s:列出背景中暂停的job

    root@DESKTOP:/mnt/# jobs -s
    [1]-  Stopped                 ping -c 100 www.qq.com
    [2]+  Stopped                 vim test.txt
    
  • 列出的job中,job number后的+表示job是被最近放入到后台中的,-代表的是倒数第二被放入到后台中的,这两个job之前放入背景的job的number后不会显示+-号,使用jobs +/-/jobnumber可以显示指定job的信息

调出在后台的job:fg命令

  • 直接执行fg命令,可以将job number后带有+号的job调回前台运行,相当于fg +

    root@DESKTOP:/mnt/# jobs -l
    [1]+  6755 Stopped                 ping -c 100 www.qq.com
    root@DESKTOP:/mnt/# fg
    ping -c 100 www.qq.com
    64 bytes from . (183.192.170.139): icmp_seq=4 ttl=55 time=14.0 ms
    64 bytes from . (183.192.170.139): icmp_seq=5 ttl=55 time=14.2 ms
    
  • 执行fg -,可以将job number后带有-号的job调回前台运行

    root@DESKTOP:/mnt/# jobs -l
    [1]-  6771 Stopped                 ping -c 100 www.google.com
    [2]+  6772 Stopped                 ping -c 100 www.qq.com
    root@DESKTOP:/mnt/# fg -
    ping -c 100 www.google.com
    64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=128 time=0.160 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=128 time=0.184 ms
    
  • fg %jobnumberfg jobnumber可以直接将指定job number的job调回前台运行

    root@DESKTOP:/mnt/# fg 2
    ping -c 100 www.qq.com
    64 bytes from . (183.192.170.170): icmp_seq=2 ttl=55 time=9.93 ms
    64 bytes from . (183.192.170.170): icmp_seq=3 ttl=55 time=9.99 ms
    

运行在后台暂停的job:bg命令

  • 直接运行bg命令会运行在后台暂停的带有+号的任务运行

    root@DESKTOP:/mnt/# jobs -l
    [1]-  6816 Stopped                 ping -c 10 www.google.com
    [4]+  6819 Stopped                 ping -c 10 www.qq.com
    [5]   6822 Running                 ping -c 100 www.sina.com > ping.txt &
    root@DESKTOP:/mnt/# bg
    [4]+ ping -c 10 www.qq.com &
    root@DESKTOP:/mnt/# 64 bytes from . (183.192.170.139): icmp_seq=2 ttl=55 time=14.1 ms
    64 bytes from . (183.192.170.139): icmp_seq=3 ttl=55 time=14.4 ms
    64 bytes from . (183.192.170.139): icmp_seq=4 ttl=55 time=14.4 ms
    64 bytes from . (183.192.170.139): icmp_seq=5 ttl=55 time=14.4 ms
    64 bytes from . (183.192.170.139): icmp_seq=6 ttl=55 time=14.3 ms
    64 bytes from . (183.192.170.139): icmp_seq=7 ttl=55 time=14.1 ms
    64 bytes from . (183.192.170.139): icmp_seq=8 ttl=55 time=14.5 ms
    64 bytes from . (183.192.170.139): icmp_seq=9 ttl=55 time=14.0 ms
    64 bytes from . (183.192.170.139): icmp_seq=10 ttl=55 time=14.6 ms
    
    --- public.sparta.mig.tencent-cloud.net ping statistics ---
    10 packets transmitted, 10 received, 0% packet loss, time 329122ms
    rtt min/avg/max/mdev = 14.009/14.322/14.627/0.225 ms
    
    [4]   Done                    ping -c 10 www.qq.com
    
  • bg %jobnumberbg jobnumber可以将指定的暂停在后台的job调出来运行

    在这里插入代码片root@DESKTOP:/mnt/# jobs -l
    [1]   6816 Stopped                 ping -c 10 www.google.com
    [2]-  6817 Stopped                 ping -c 10 www.qq.com
    [3]+  6818 Stopped                 ping -c 10 www.sina.com
    root@DESKTOP:/mnt/# bg 2
    [2]- ping -c 10 www.qq.com &
    root@DESKTOP:/mnt/# 64 bytes from . (183.192.170.139): icmp_seq=2 ttl=55 time=14.2 ms
    64 bytes from . (183.192.170.139): icmp_seq=3 ttl=55 time=14.0 ms
    64 bytes from . (183.192.170.139): icmp_seq=4 ttl=55 time=14.0 ms
    
  • jobs ; bg 3 ; jobs

    root@DESKTOP:/mnt/# jobs ; bg 3 ; jobs
    [1]   Stopped                 ping -c 10 www.google.com
    [3]-  Stopped                 ping -c 10 www.sina.com
    [4]+  Stopped                 ping -c 10 www.qq.com
    [3]- ping -c 10 www.sina.com &
    [1]-  Stopped                 ping -c 10 www.google.com
    [3]   Running                 ping -c 10 www.sina.com &
    [4]+  Stopped                 ping -c 10 www.qq.com
    

杀掉后台中的工作:kill命令

  • kill -1 PID:重读配置文件,类似reload
  • kill -9 PID:强制杀掉指定PID的进程
  • kill -15 PID:以正常方式结束指定PID的进程。15也是默认值
  • kill -signal %jobnumber:以指定的信号杀掉指定jobnumber的进程。指定jobnumber要加%,因为kill后面默认是PID。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值