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.

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.

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).

nohup ./long-running-task.sh

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

nohup ./backup.sh &

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

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

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

nohup --version

Ausgabe umleiten

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

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

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

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).

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.

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).

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.

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).

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

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

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.

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.

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.

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'.

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.

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.

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).

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.

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.

ps -ef | grep worker.sh

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

tail -f nohup.out

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

tail -f worker.log

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

kill 4242

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

kill -9 4242

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

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.

long-task & disown

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

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.

tmux new -d -s build 'make all'

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

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.

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.

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.

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.

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.

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

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.

Verwandte Kommandos

  • jobs – zeigt Hintergrund- und angehaltene Jobs der aktuellen Shell
  • screen – Terminal-Multiplexer mit ablös- und wieder anhängbaren Sitzungen
  • tmux – moderner Terminal-Multiplexer mit Reattach und Fenster-Splits