Redirecting stdout & stderr from background process

时光怂恿深爱的人放手 提交于 2019-12-05 16:22:03
nohup foo &

When you exit your shell it sends a SIGHUP signal to all child processes, which by default kills them. If you want a process to continue executing even when the parent shell exits then you need to have it ignore the SIGHUP.

NAME

nohup -- invoke a command immune to hangups

SYNOPSIS

nohup utility [arg ...]

DESCRIPTION

The nohup utility invokes command with its arguments and at this time sets the signal SIGHUP to be ignored. If the standard output is a terminal, the standard output is appended to the file nohup.out in the current directory. If standard error is a terminal, it is directed to the same place as the standard output.

Since the question is tagged as bash as well, I quote from man bash

disown [-ar] [-h] [jobspec ...]
          Without  options,  each  jobspec  is  removed  from the table of
          active jobs.  If jobspec is not present, and neither -a  nor  -r
          is  supplied, the shell's notion of the current job is used.  If
          the -h option is given, each jobspec is not removed from the ta‐
          ble,  but is marked so that SIGHUP is not sent to the job if the
          shell receives a SIGHUP.  If no jobspec is present, and  neither
          the  -a  nor the -r option is supplied, the current job is used.
          If no jobspec is supplied, the -a option means to remove or mark
          all  jobs;  the  -r  option without a jobspec argument restricts
          operation to running jobs.  The return value is 0 unless a  job‐
          spec does not specify a valid job.

This comes in handy when you started a job but forgot to prefix it with nohup. Just do

disown -ah
disown -a

Don't forget to remove all references to the tty/pts, 0</dev/null removes the stdin reference.

Your a.exe gets killed when you close the parent shell.

You could type screen, run the command as usual and exit the screen. This keeps the process from getting killed when the parent shell is exits. This method is a little cumbersome but might be handy in other situations.

John gave a much better answer - use nohup.

khachik

Try:

nohup <your_command> &
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!