Fundamentals 5 min read

How to Keep Long‑Running Linux Jobs Alive with nohup

This guide explains why long‑running programs stop when a terminal closes, introduces the nohup command to ignore hang‑up signals, and provides step‑by‑step examples for running, redirecting output, backgrounding, chaining multiple commands, and terminating nohup‑started processes on Linux.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Keep Long‑Running Linux Jobs Alive with nohup

Why use nohup?

When a program runs for hours or days, closing the terminal or losing network connectivity normally sends a HUP (hang‑up) signal that terminates the process. The nohup command (short for “no hangup”) tells the shell to ignore this signal, allowing the program to continue running.

Basic syntax

$ nohup command arguments

or $ nohup options Use nohup --help for help and nohup --version to see the version.

Running a program with nohup

Execute a command with nohup so it keeps running after the shell exits: $ nohup my_program The standard output and error are written to a file named nohup.out in the current directory (or home directory if not writable).

Redirecting output

To store output in a custom location and filename, redirect it with >:

$ nohup ./myScript.sh > ~/output/myOutput.txt

Background execution

Append & to run the job in the background. The process will still be detached from the terminal. Use fg to bring it back to the foreground if needed.

Running multiple commands

Chain several commands with && inside a bash -c string, and redirect the combined output:

$ nohup bash -c 'mkdir files && ping -c 1 baidu.com && ls' > output.txt

Stopping a background process

Find the process ID (PID) with ps or pgrep, then terminate it with kill -9 PID:

$ ps aux | grep myScript.sh
$ kill -9 14942
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellbackground executionnohup
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.