有时候咱们运行一个程序,耗时比较长,因此在快下班的时候或是网络不稳定的时候就比较抓狂。 今天分享几个我在工做中用到的把程序放在后台运行的方法。bash
$ 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. 在使用过程当中有几个小技巧:网络
使用方法:nohup command &session
在退出当前帐户或是关闭终端的时候程序会收到SIGHUP信号,这个信号会被使用了nohup的程序忽略,可是若是使用了CTRL + C 结束命令,程序会收到SIGINT信号,这个信号仍是会让程序终结,若是不想程序被结束,就在结尾加上一个&, 让程序也忽略SIGINT 信号this
使用方法: nohup command > filename 2<&1 &orm
这里的1,2是文件描述符,0表示stdin标准输入,1表示stdout标准输出,2表示stderr标准错误, 还有一个/dev/null 表示空设备文件。blog
nohup 默认会把输出输出到nohup.out文件中,若是想重定向输出到别的文件,那么须要在nohup command后加入 > filename, 2<&1,表示把标准错误重定向到标准输出中。进程
$ 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能够帮助咱们作到这一点。terminal
使用方法: setsid command it