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

    感谢大佬:https://www.cnblogs.com/baibai-tech/p/8918758.html

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

    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

  • 相关阅读:
    ubuntu修改文件访问权限
    ubuntu使用root账户登录
    centos7 列出所有系统服务
    virtualbox 虚拟机硬盘扩容
    CI的意思
    更改centos 7 的默认启动为命令界面
    git Staging Deleted files
    Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
    List of data structures:数据结构大全
    List of algorithms:算法大全
  • 原文地址:https://www.cnblogs.com/tfxz/p/12621565.html
Copyright © 2011-2022 走看看