Amblem
Furkan Baytekin

Orphan Your Children (Processes): Using nohup and disown

Orphan Your Processes: nohup & disown in Linux

Orphan Your Children (Processes): Using nohup and disown
68
3 minutes

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:

  1. Start a long-running command with nohup:
bash
nohup python3 -m http.server 8080 &
  1. 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:

bash
nohup 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:

bash
cat server.log

This ensures all output is neatly organized and retrievable even after logging out.

  1. 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:

bash
ps 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:

  1. Start a command normally:
bash
sleep 300
  1. Send it to the background:
bash
Ctrl+Z bg
  1. Detach it from the shell:
bash
disown

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:

  1. Start a command with nohup:
bash
nohup ./my_script.sh &
  1. Detach it using disown:
bash
disown

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:

  1. Find the process ID (PID):
bash
ps aux | grep my_script.sh
  1. Kill the process:
bash
kill <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!

Suggested Blog Posts