zoukankan      html  css  js  c++  java
  • Linux --- 程序后台运行的几种方法

    有时候我们运行一个程序,耗时比较长,所以在快下班的时候或是网络不稳定的时候就比较抓狂。 今天分享几个我在工作中用到的把程序放在后台运行的方法。

    nohup 

    $ nohup --h
    Usage: nohup COMMAND [ARG]...
      or:  nohup OPTION
    Run COMMAND, ignoring hangup signals.
    
          --help     display this help and exit
          --version  output version information and exit
    

    当运行一个程序的时候,我们在退出当前账户,或者关闭终端的时候,程序都会收到一个SIGHUP的信号,nohup就是忽略这种信号,让程序不挂起。 nohup使用方法很简单,即:nohup command. 在使用过程中有几个小技巧:

    1. 同时使用nohup和&

    使用方法:nohup command &

    在退出当前账户或是关闭终端的时候程序会收到SIGHUP信号,这个信号会被使用了nohup的程序忽略,但是如果使用了CTRL + C 结束命令,程序会收到SIGINT信号,这个信号还是会让程序终结,如果不想程序被结束,就在结尾加上一个&, 让程序也忽略SIGINT 信号

    2. 重定向标准输出和标准错误

    使用方法: nohup command > filename 2<&1 &

    这里的1,2是文件描述符,0表示stdin标准输入,1表示stdout标准输出,2表示stderr标准错误, 还有一个/dev/null 表示空设备文件。

    nohup 默认会把输出输出到nohup.out文件中,如果想重定向输出到别的文件,那么需要在nohup command后加入 > filename, 2<&1,表示把标准错误重定向到标准输出中。

    setsid

    $ setsid --h
    
    Usage:
     setsid [options] <program> [arguments ...]
    
    Run a program in a new session.
    
    Options:
     -c, --ctty     set the controlling terminal to the current one
     -w, --wait     wait program to exit, and use the same return
     -h, --help     display this help and exit
     -V, --version  output version information and exit
    

      

    nohup 通过忽略HUP信号使进程避免中途被中断,如果我们的进程不属于接受HUP信号的终端子进程,那么就不会受HUP信号的影响。使用setsid可以帮助我们做到这一点。

    使用方法: setsid command 

  • 相关阅读:
    函数指针
    动态内存
    char*和char[]的区别
    C语言基本数据类型大小
    html5新特性localStorage和sessionStorage
    Swoole实现h5版聊天室笔记
    php使用mysql之sql注入(功)
    Http协议工作特点和工作原理笔记
    原生js使用ajax
    php常用的几个预定义变量
  • 原文地址:https://www.cnblogs.com/baibai-tech/p/8918758.html
Copyright © 2011-2022 走看看