Redirecting stdout & stderr from background process Redirecting stdout & stderr from background process bash bash

Redirecting stdout & stderr from background process


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 -ahdisown -a


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