---
title: "Orphan Your Children (Processes): Using nohup and disown"
description: "Learn to use nohup and disown to keep processes running in Linux even after logout. Practical terminal examples included."
slug: "orphan-your-children-processes-using-nohup-and-disown"
url: "https://furkanbaytekin.dev/blogs/orphan-your-children-processes-using-nohup-and-disown"
author: "Furkan Baytekin"
category: "Software"
tags: []
published: 2024-12-30T10:29:32Z
updated: 2024-12-30T10:29:32Z
readTime: 3
cover: "https://myblogcms.furkanbaytekin.dev/uploads/cover/orphan-your-children-processes-using-nohup-and-disown+1735309832125"
---

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 &
```

2. 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.

3. 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
```

2. Send it to the background:

```bash
Ctrl+Z
bg
```

3. 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 &
```

2. 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
```

2. 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!
