When working with long-running processes in Linux, you might want to keep a program running even after you log out. In such cases, tools like nohup
and disown
are your best friends. This post will explain what these tools do and provide practical terminal examples to help you get started.
What Happens to Child Processes When a Parent Logs Out?
In Linux, every process is forked from a parent process, creating a hierarchy where child processes depend on their parent. When you log out of a terminal session, the shell sends a SIGHUP
(hangup signal) to all running child processes. If a child process loses its parent, it will not automatically continue running; instead, it receives the SIGHUP
signal and is terminated unless specific measures like using nohup
or disown
are taken to prevent this. To intentionally orphan a process and ensure its continuation, you must detach it from the terminal before logging out.
Meet nohup
nohup
stands for βno hangup.β It prevents the SIGHUP
signal from being sent to a process when you log out.
Example:
-
Start a long-running command with
nohup
:
bashnohup python3 -m http.server 8080 &
-
This starts a Python HTTP server on port 8080 in the background. The output will be redirected to a file named
nohup.out
unless specified otherwise.
Output:
nohup: ignoring input and appending output to 'nohup.out'
For example, to redirect output to a specific file and avoid the default nohup.out
, you can use:
bashnohup python3 -m http.server 8080 > server.log 2>&1 &
This will redirect both standard output and error messages to server.log
. Later, you can check the log file:
bashcat server.log
This ensures all output is neatly organized and retrievable even after logging out.
-
You can now safely log out, although the terminal might warn you about running processes. In that case, use the
logout
command with the-f
(force) option to proceed, and the server will continue running.
To verify the process is still running, you can SSH back into the server and use:
bashps aux | grep "python3 -m http.server"
Enter disown
disown
is a shell builtin command used to detach a running process from the terminal. Unlike nohup
, it doesnβt need to be invoked when starting the command.
Example:
- Start a command normally:
bashsleep 300
- Send it to the background:
bashCtrl+Z
bg
- Detach it from the shell:
bashdisown
Now, the process will continue running even if you close the terminal.
Combining nohup
and disown
For maximum reliability, you can use both nohup
and disown
together:
Example:
-
Start a command with
nohup
:
bashnohup ./my_script.sh &
-
Detach it using
disown
:
bashdisown
This ensures the process is fully independent of your terminal session.
Cleaning Up: Killing Detached Processes
Sometimes you might need to stop an orphaned process. Use the following command to identify and kill it:
- Find the process ID (PID):
bashps aux | grep my_script.sh
- Kill the process:
bashkill <PID>
Conclusion
Using nohup
and disown
is a simple yet effective way to keep processes running even after you log out of a terminal. With these tools in your arsenal, you can confidently run long-running tasks without worrying about session interruptions. Experiment with these commands to become more efficient in managing background processes!