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.
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:
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:
- 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. - Next, use
bgto resume the process in the background:
- Then, find the job ID using the
jobscommand. Look for the process/job in the list. - Finally, apply
disownto the job:
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
- Planning for Long-Running Processes: For processes anticipated to run long, always consider starting them with
nohupor within a session usingscreenortmux. - Resource Management: Ensure that disowned processes or those under
nohupwon't excessively consume system resources over time without monitoring.
Summary Table
| Method | Command Sequence | Pros | Cons |
nohup | nohup command & | Simple and direct | Must be used at command start |
disown | Ctrl+Z, bg, disown %job_id | Can be used anytime | Slightly complex; purely command line |
screen/tmux | Start screen/tmux, then run command | Powerful & flexible; session reattachment possible | Requires 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.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.