Friday, October 29, 2021

[SOLVED] How to use nohup on a process already running in background

Issue

So I have a job running in Background on my unix terminal, I will have to close the ssh terminal after some time and my job has to continue after that as well. How can I use the nohup or any other possible option to achieve this.


Solution

nohup starts a new process. You cannot retroactively apply it to a process that you've already started.

However, if the shell from which you launched the job is bash, ksh, or zsh then the disown job-control builtin may provide what you want. It can either remove the job from job control altogether or just flag the job to not be sent a SIGHUP when the parent shell itself receives one. This is similar, but not necessarily identical, to the effect of starting a process via the nohup command.

Note well that your job may still have issues if any of its standard streams is connected to the session's terminal. That's something that nohup typically clobbers preemptively, but disown cannot modify after the fact. You're normally better off anticipating this need and starting the process with nohup, but if you're not so foresightful then disown is probably your next best bet.

Note also that as a job-control command, disown takes a jobspec to identify the job to operate on, not a process ID. If necessary, you can use the jobs builtin to help determine the appropriate jobspec.



Answered By - John Bollinger