nohup
process management
Linux commands
background process
Unix shell

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

When working on a Linux or Unix-like system, you might start a process that runs longer than expected or realize that it needs to continue running after your terminal session ends. Normally, if the terminal is closed, the process is terminated as well. The nohup command is a conventional solution, designed to keep a command running even after the terminal is closed. However, nohup must be invoked at the beginning when starting a process. If you didn't start the process with nohup, there are still ways to disassociate the process from the terminal session.

Understanding nohup and Terminal Hangups

nohup, which stands for "no hang up," ignores the HUP (hangup) signal. This signal is sent to the process when its controlling terminal is closed. When a process is started with nohup, it's effectively shielded from this hangup signal, allowing it to continue running in the background even after the user logs out.

Below are the typical steps for using nohup:

bash
nohup long-running-command &

This command starts long-running-command, ignores the hangup signal and puts the process in the background. The output of the command, if not redirected, is sent to a file named nohup.out in the current directory.

Handling an Already-Running Process

For processes that weren't started with nohup, we need a workaround since nohup cannot be directly applied to them. The main tools to handle this situation are disown and the ability to reattach sessions with screen or tmux.

Using disown

disown is a shell builtin in Bash and Z Shell. It removes a shell job from the shell's job table, effectively giving it independence from the terminal. Even if the shell session is terminated, the disown-ed process continues.

Here's how to use disown:

  1. First, pause (suspend) the running process by typing Ctrl+Z. This sends the SIGTSTP (Signal Stop) to the foreground process and moves it into the background.
  2. Next, use bg to resume the process in the background:
bash
   bg
  1. Then, find the job ID using the jobs command. Look for the process/job in the list.
  2. Finally, apply disown to the job:
bash
   disown %job_id

Replace job_id with the actual job ID.

Using screen or tmux

Both screen and tmux are terminal multiplexers, allowing you to detach and reattach sessions. While they cannot directly put an already-running outside process under their control, they are helpful if you start inside these tools from the beginning.

Best Practices and Considerations

  1. Planning for Long-Running Processes: For processes anticipated to run long, always consider starting them with nohup or within a session using screen or tmux.
  2. Resource Management: Ensure that disowned processes or those under nohup won't excessively consume system resources over time without monitoring.

Summary Table

MethodCommand SequenceProsCons
nohupnohup command &Simple and directMust be used at command start
disownCtrl+Z, bg, disown %job_idCan be used anytimeSlightly complex; purely command line
screen/tmuxStart screen/tmux, then run commandPowerful & flexible; session reattachment possibleRequires prior use & familiarity

Conclusion

Putting an already-running process under nohup isn't directly possible, but Linux provides powerful alternatives like disown, along with tools like screen and tmux for managing terminal sessions. Understanding these tools enhances your ability to manage long-running processes and ensure they continue running smoothly, irrespective of the terminal's state.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions