zoukankan      html  css  js  c++  java
  • 如何让程序在后台执行

    示例:查看系统负载的脚本

    #!/bin/sh
    
    while true
    do
      uptime >/tmp/uptime.log
      sleep 1
    done
    [root@lamp scripts]# tail -f /tmp/uptime.log 
     21:37:26 up  5:49,  2 users,  load average: 0.00, 0.00, 0.00
    tail: /tmp/uptime.log: file truncated
     21:37:27 up  5:49,  2 users,  load average: 0.00, 0.00, 0.00
    tail: /tmp/uptime.log: file truncated
     21:37:28 up  5:49,  2 users,  load average: 0.00, 0.00, 0.00
    tail: /tmp/uptime.log: file truncated

    当我们执行该脚本的时候:

    会出现在登录的界面卡顿的情况,无法执行其他的命令,此时如果出现网络中断(用户掉线)任务就会停止;所以我们在执行的时候会加上“&”符号,意为在后台执行,当用户退出的时候该任务仍会执行。

    [root@lamp scripts]# sh test.sh  
    
    
    
    ^C
    [root@lamp scripts]# sh test.sh &
    [1] 25352
    [root@lamp scripts]# 

    但是,当我们想停止任务的时候怎么办?

    [root@lamp scripts]# sh test.sh &
    [1] 25352
    [root@lamp scripts]# jobs
    [1]+  Running                 sh test.sh &
    [root@lamp scripts]# fg 1    //为任务号
    sh test.sh
    ^C
    [root@lamp scripts]# 

    有时候我们想将在前台执行的任务转为后台执行怎么办?

    [root@lamp scripts]# sh test.sh  
    
    ^Z
    [1]+  Stopped                 sh test.sh
    [root@lamp scripts]# bg 1         //1为任务号
    [1]+ sh test.sh &
    [root@lamp scripts]# jobs
    [1]+  Running                 sh test.sh &
    [root@lamp scripts]# 

    让程序后台执行的几种方法:

      1.sh test.sh &

      2.screen

      3.nohup test.sh &

  • 相关阅读:
    SPOJ NSUBSTR
    一点对后缀自动机的理解 及模板
    HDU 1086 You can Solve a Geometry Problem too
    HDU2036 改革春风吹满地
    POJ 2318 TOYS
    [HNOI2008]玩具装箱TOY
    HDU 3507 Print Article
    洛谷 P1231 教辅的组成(网络最大流+拆点加源加汇)
    P3984 高兴的津津
    P2756 飞行员配对方案问题(网络流24题之一)
  • 原文地址:https://www.cnblogs.com/along1226/p/4989797.html
Copyright © 2011-2022 走看看