Linux – how to convert a running process into a nohup and keep a logfile

bashcommand-line-interfacelinuxnohupunix

I have a process currently occupying a terminal

]$ command some_argument

I want to exit from the terminal and go home, but in the mean time I don't want to kill this running process.

For the above running process, I want to achieve something like the following:

]$ nohup command some_argument >& logfile &

Best Answer

Disowning the process and redirecting STDOUT from an already backgrounded process isn't feasible for most users. This SO answer covers the topic pretty well.

Disassociating the process from the current shell isn't so hard: you're looking for disown. From the manpage:

SIGNALS

....

       The shell exits by default upon receipt of a SIGHUP.   Before  exiting,
       an  interactive  shell  resends  the  SIGHUP  to  all  jobs, running or
       stopped.  Stopped jobs are sent SIGCONT to ensure that they receive the
       SIGHUP.   To  prevent the shell from sending the signal to a particular
       job, it should be removed from the jobs table with the  disown  builtin
       (see  SHELL  BUILTIN  COMMANDS  below)  or marked to not receive SIGHUP
       using disown -h.

....

SHELL BUILTIN COMMANDS

....

       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.

If you want to learn more about what nohup and disown actually do, you can check out this question. (disclaimer: shameless self-promotion)