0
0
Linux CLIscripting~5 mins

nohup for persistent processes in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want a program to keep running even after you close the terminal. The nohup command helps you do this by ignoring the hangup signal, so your process keeps working in the background.
When you start a long script on a remote server and want it to keep running after you disconnect.
When you launch a backup job that takes hours and you don't want to keep the terminal open.
When running a server or service manually and want it to stay alive after logout.
When testing a command that might take a long time and you want to avoid accidental termination.
When you want to save the output of a command to a file while it runs in the background.
Commands
This command runs the 'sleep 60' command in the background and uses nohup to keep it running even if the terminal closes.
Terminal
nohup sleep 60 &
Expected OutputExpected
nohup: ignoring input and appending output to 'nohup.out' [1] 12345
This command checks if the 'sleep' process is still running after starting it with nohup.
Terminal
ps -ef | grep sleep
Expected OutputExpected
user 12345 6789 0 12:00 pts/0 00:00:00 sleep 60 user 12346 1234 0 12:00 pts/0 00:00:00 grep sleep
This command shows the output saved by nohup. Since 'sleep' produces no output, the file will be empty or not created.
Terminal
cat nohup.out
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: nohup lets a process keep running after you close the terminal by ignoring hangup signals.

Common Mistakes
Running a command with nohup but forgetting to add '&' to run it in the background.
The command will run but the terminal will be blocked until it finishes, defeating the purpose of nohup for persistent background processes.
Always add '&' at the end to run the command in the background, like 'nohup command &'.
Not checking the nohup.out file for output or errors.
You might miss important messages or errors because nohup redirects output to nohup.out by default.
Check the nohup.out file or redirect output explicitly to a file you choose.
Summary
Use 'nohup command &' to run a command that keeps running after terminal closes.
Check running processes with 'ps -ef | grep command' to confirm it's still active.
Look at 'nohup.out' for any output or errors the command produces.