0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use nohup Command in Linux: Syntax and Examples

Use the nohup command in Linux to run a process that keeps running even after you log out. The basic syntax is nohup command &, where command is what you want to run and & runs it in the background.
📐

Syntax

The basic syntax of the nohup command is:

  • nohup: Prevents the process from stopping when the terminal closes.
  • command: The command or script you want to run.
  • &: Runs the command in the background so you can keep using the terminal.

Output that would normally go to the terminal is saved to nohup.out by default.

bash
nohup command &
💻

Example

This example runs a simple script that prints numbers every second. It uses nohup to keep running after logout and sends the process to the background.

bash
nohup bash -c 'for i in {1..5}; do echo "Number $i"; sleep 1; done' &
Output
nohup: ignoring input and appending output to 'nohup.out' [1] 12345
⚠️

Common Pitfalls

Common mistakes when using nohup include:

  • Not adding & to run the command in the background, which keeps the terminal busy.
  • Forgetting that output goes to nohup.out if not redirected, so you might not see logs immediately.
  • Running commands that require user input, which will hang because nohup ignores input.

Correct usage example:

bash
# Wrong: runs in foreground and blocks terminal
nohup sleep 30

# Right: runs in background
nohup sleep 30 &
📊

Quick Reference

OptionDescription
nohup command &Run command immune to hangups, in background
nohup command > output.log 2>&1 &Run command, redirect output to file, background
kill PIDStop the background process by its process ID

Key Takeaways

Use nohup command & to run a process that keeps running after logout.
Add & to run the command in the background and free the terminal.
Output goes to nohup.out by default unless redirected.
Avoid commands that require user input when using nohup.
Check running processes with ps and stop them with kill if needed.