# nohup — Prozesse vom Terminal abkoppeln

> nohup koppelt Befehle vom Terminal ab, sodass sie SIGHUP ignorieren und nach dem Logout weiterlaufen – ideal für Backups und lange Jobs per SSH.

Source: https://www.jpkc.com/db/cheatsheets/shell-system/nohup/

<!-- PROSE:intro -->
Wenn du einen langlaufenden Befehl über SSH startest und die Verbindung abbricht, ist der Prozess normalerweise tot – beim Schließen des Terminals reißt ihn das SIGHUP-Signal mit. `nohup` schirmt genau dieses Signal ab: Der Befehl ignoriert SIGHUP und läuft weiter, auch nach dem Logout. Kombiniert mit `&` für den Hintergrund und einer Ausgabeumleitung wird daraus die klassische Methode, einen Job vom Terminal abzukoppeln.
<!-- PROSE:intro:end -->

## Grundlagen

`nohup <command>` — Führt einen Befehl resistent gegen Hangups aus. Die Ausgabe wird an ./nohup.out angehängt (oder $HOME/nohup.out, falls das Arbeitsverzeichnis nicht beschreibbar ist).

```bash
nohup ./long-running-task.sh
```

`nohup <command> &` — Läuft im Hintergrund UND ignoriert SIGHUP – überlebt das Schließen des Terminals. Der typische Aufruf.

```bash
nohup ./backup.sh &
```

`nohup <command> arg1 arg2 &` — Übergibt dem Befehl wie gewohnt Argumente.

```bash
nohup rsync -av /data/ user@host:/backup/ &
```

`nohup --version` — Zeigt die Version der GNU coreutils (BusyBox/macOS-nohup unterstützen --version nicht).

```bash
nohup --version
```

## Ausgabe umleiten

`nohup <command> > out.log 2>&1 &` — Leitet sowohl stdout als auch stderr in eine eigene Logdatei statt nach nohup.out.

```bash
nohup ./worker.sh > worker.log 2>&1 &
```

`nohup <command> > out.log 2> err.log &` — Trennt stdout und stderr in unterschiedliche Dateien.

```bash
nohup ./build.sh > build.log 2> build.err &
```

`nohup <command> >> out.log 2>&1 &` — Hängt an eine bestehende Logdatei an (nützlich beim Neustart eines Jobs).

```bash
nohup ./service.sh >> /var/log/service.log 2>&1 &
```

`nohup <command> > /dev/null 2>&1 &` — Verwirft sämtliche Ausgabe. Üblich für Fire-and-Forget-Aufgaben.

```bash
nohup ./cleanup.sh > /dev/null 2>&1 &
```

`nohup <command> < /dev/null > out.log 2>&1 &` — Leitet zusätzlich stdin von /dev/null um – koppelt vollständig vom Terminal ab (kein SIGTTIN, wenn das Terminal geschlossen wird).

```bash
nohup ./server.sh < /dev/null > server.log 2>&1 &
```

## PID festhalten

`nohup <command> > out.log 2>&1 & echo $!` — Gibt die PID des Hintergrund-Jobs aus. $! ist die PID des zuletzt gestarteten Hintergrundprozesses.

```bash
nohup ./worker.sh > worker.log 2>&1 & echo $!
```

`nohup <command> > out.log 2>&1 & echo $! > task.pid` — Speichert die PID in einer Datei für die spätere Verwaltung (Beenden, Statusabfragen).

```bash
nohup ./worker.sh > worker.log 2>&1 & echo $! > worker.pid
```

`kill $(cat task.pid)` — Beendet einen zuvor gestarteten nohup-Job über die gespeicherte PID.

```bash
kill $(cat worker.pid)
```

## Pipelines und Shell-Skripte

`nohup bash -c '<cmd1> | <cmd2>' &` — nohup schützt nur den ERSTEN Befehl. Verpacke Pipelines oder Umleitungen in 'bash -c', damit die gesamte Pipeline geschützt ist.

```bash
nohup bash -c 'tail -f app.log | grep ERROR > errors.log' &
```

`nohup bash -c '<cmd1>; <cmd2>; <cmd3>' &` — Führt eine Befehlsfolge in einer einzigen nohup-geschützten Shell aus.

```bash
nohup bash -c './backup.sh; ./upload.sh; ./cleanup.sh' &
```

`nohup ./script.sh &` — Führt ein Skript aus, das bereits Pipelines/Schleifen enthält. Innerhalb des Skripts muss nur das Skript selbst per nohup geschützt werden.

```bash
nohup ./deploy.sh > deploy.log 2>&1 &
```

## Kombination mit anderen Werkzeugen

`nohup <command> & disown` — Doppelt hält besser: nohup ignoriert SIGHUP, disown entfernt den Job aus der Jobtabelle der Shell. Überlebt selbst gesetztes 'shopt -s huponexit'.

```bash
nohup ./server.sh > server.log 2>&1 & disown
```

`setsid nohup <command> &` — Startet in einer neuen Session UND ignoriert SIGHUP – vollständig von der Terminal-Session abgekoppelt.

```bash
setsid nohup ./daemon.sh > daemon.log 2>&1 &
```

`nice -n <prio> nohup <command> &` — Führt einen langlebigen Hintergrund-Job mit niedrigerer CPU-Priorität aus.

```bash
nice -n 19 nohup ./compress-archives.sh &
```

`ionice -c 3 nohup <command> &` — Führt mit Idle-I/O-Priorität aus (greift nur auf die Platte zu, wenn sonst nichts sie braucht).

```bash
ionice -c 3 nohup ./backup.sh &
```

`ssh user@host 'nohup <command> > out.log 2>&1 &'` — Startet per SSH einen entfernten Hintergrund-Job, der das Schließen der SSH-Sitzung überlebt.

```bash
ssh deploy@web01 'nohup ./restart.sh > restart.log 2>&1 &'
```

## Prüfen und Beenden

`ps -ef | grep <command>` — Prüft, ob der nohup-Prozess noch läuft.

```bash
ps -ef | grep worker.sh
```

`tail -f nohup.out` — Verfolgt das Standard-Ausgabelog live.

```bash
tail -f nohup.out
```

`tail -f <custom.log>` — Verfolgt eine eigene Logdatei live.

```bash
tail -f worker.log
```

`kill <pid>` — Sendet SIGTERM an einen nohup-Prozess anhand der PID.

```bash
kill 4242
```

`kill -9 <pid>` — Erzwingt das Beenden (SIGKILL) eines Prozesses, der sich nicht sauber beenden lässt.

```bash
kill -9 4242
```

`pkill -f <pattern>` — Beendet anhand eines Kommandozeilen-Musters, wenn du die PID nicht gespeichert hast.

```bash
pkill -f worker.sh
```

## nohup im Vergleich zu Alternativen

`<command> & disown` — disown allein entfernt den Job aus der Jobtabelle der Shell. Ohne nohup wird SIGHUP weiterhin zugestellt, wenn 'huponexit' aktiv ist.

```bash
long-task & disown
```

`setsid <command>` — Startet den Befehl in einer neuen Session – kein kontrollierendes Terminal, sodass SIGHUP ihn nicht erreichen kann.

```bash
setsid ./daemon.sh
```

`tmux new -d -s <name> '<command>'` — Läuft in einer abgekoppelten tmux-Session – behält ein TTY für späteres interaktives Anhängen.

```bash
tmux new -d -s build 'make all'
```

`screen -dmS <name> <command>` — Entsprechung für GNU Screen – Start in einer abgekoppelten, benannten Session.

```bash
screen -dmS build make all
```

`systemd-run --user --unit=<name> <command>` — Läuft als transiente systemd-User-Unit – mit Logging über journalctl und Lebenszyklus-Steuerung über systemctl.

```bash
systemd-run --user --unit=build make all
```

## Praktische Rezepte

`nohup <command> > out.log 2>&1 < /dev/null & echo $!` — Robuste Vorlage: von stdin abgekoppelt, alle Ausgaben in eine Datei, PID für die spätere Verwaltung ausgegeben.

```bash
nohup ./worker.sh > worker.log 2>&1 < /dev/null & echo $!
```

`Laufenden Vordergrund-Job nachträglich abkoppeln` — Befehl läuft bereits im Vordergrund? Anhalten, in den Hintergrund schicken, dann disown – Hinweis: nohup lässt sich nicht nachträglich anwenden.

```bash
Ctrl+Z
bg
disown -h %1
```

`ssh -n -f host 'nohup <cmd> > /tmp/out 2>&1 &'` — Fire-and-Forget per SSH: -n leitet stdin von /dev/null um, -f schickt ssh nach der Authentifizierung in den Hintergrund.

```bash
ssh -n -f deploy@web01 'nohup ./deploy.sh > /tmp/deploy.log 2>&1 &'
```

`Cron ohne Log-Spam` — In cron ist nohup überflüssig (cron hat kein kontrollierendes Terminal). Nutze einfache Umleitung – halte es schlicht.

```bash
*/5 * * * * /usr/local/bin/job.sh > /var/log/job.log 2>&1
```

<!-- PROSE:outro -->
## Fazit

`nohup` ist die schnellste Art, einen Befehl vom Terminal abzukoppeln: ein vorangestelltes `nohup`, ein `&` für den Hintergrund, fertig – der Job überlebt Logout und SSH-Abbruch. Für mehr Robustheit leitest du die Ausgabe gezielt um (`> log 2>&1`), statt dich auf `nohup.out` zu verlassen, koppelst stdin von `/dev/null` ab und sicherst dir die PID per `echo $!`. Eine Grenze solltest du kennen: nohup kennt kein Reattach – willst du später wieder in die laufende Sitzung schauen, sind `screen` oder `tmux` die bessere Wahl, und für echte, dauerhafte Dienste gehört der Prozess in eine `systemd`-Unit statt hinter nohup.

## Weiterführende Links

- [GNU Coreutils: nohup invocation](https://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html) – offizielle Referenz zur GNU-Implementierung von nohup
- [ubuntuusers-Wiki: Shell](https://wiki.ubuntuusers.de/Shell/) – deutschsprachige Einführung in Shell, Jobsteuerung und Hintergrundprozesse
<!-- PROSE:outro:end -->

## Verwandte Kommandos

- [jobs](https://www.jpkc.com/db/cheatsheets/shell-system/jobs/) – zeigt Hintergrund- und angehaltene Jobs der aktuellen Shell
- [screen](https://www.jpkc.com/db/cheatsheets/shell-system/screen/) – Terminal-Multiplexer mit ablös- und wieder anhängbaren Sitzungen
- [tmux](https://www.jpkc.com/db/cheatsheets/shell-system/tmux/) – moderner Terminal-Multiplexer mit Reattach und Fenster-Splits

