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.
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 argumentsor $ 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.txtBackground 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.txtStopping 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 14942Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
