How do I put an already-running process under nohup?

Overview

If a process is running and we don't want to stop it, even after closing the terminal, then we put it under nohup, so that it will still execute even after closing the terminal.

We can achieve this by using the following command.

Syntax

# disowns all processes
disown -a

We need to follow the process below to make it work.

  • Run our command in the first tab of our terminal.
  • While running, we open another tab of our terminal and run the command disown -a.
  • Then, we close the first terminal. Then our process will still run in the background even after closing the terminal.

Let's take a look at an example.

Example

We'll use the following code snippet, which takes a long time to execute.

for num in {1..1000000}
do
echo $num
done | tee my_job.log

As specified in the above steps,

  • Connect to the first terminal.
  • After a successful connection, we'll get a + sign to initiate the second terminal. We'll also need to open the second terminal.
  • Now, we run the above code snippet in the first terminal, then go to the second terminal, and execute disown -a, while the code still executing in the first terminal.
  • Then, we close the first terminal.
  • Since the process is disowned, it will still execute in the background even after closing the terminal.
  • We can verify this by printing, cat my_job.log, the log file we stored.
  • We can view the output by the command that is executed even after we closed the terminal.
    Terminal 1
    Terminal
    Loading...

    Free Resources