Skip to content

nohup Command in Linux

Summary

The nohup command in Linux allows you to run a command that will continue to execute even after you close the terminal or log out. It prevents the command from receiving the SIGHUP (hang up) signal, which is typically sent when the terminal is closed.

Introduction

The nohup command is a crucial tool for running long-lasting processes in the background on a Linux system. Without nohup, closing your terminal window usually terminates any processes you started within it. nohup redirects standard output and standard error to a file (usually nohup.out) if they are not already redirected, ensuring the command's output is preserved. This makes it perfect for tasks like running server applications, performing extensive data processing, or executing scripts that need to continue without user interaction.

Use case and Examples

Running a script in the background indefinitely

nohup ./my_script.sh &
This command executes the my_script.sh script in the background. The & symbol places the process in the background, and nohup ensures it continues running even if you close your terminal. Output is redirected to nohup.out if not already redirected by the script.

Running a command with output redirected to a specific file

nohup ./my_script.sh > my_output.log 2>&1 &
This example redirects both standard output (> my_output.log) and standard error (2>&1) to the my_output.log file. This is useful for capturing the entire output of the script in a single file. The & puts the process in the background.

Suppressing output completely

nohup ./my_script.sh > /dev/null 2>&1 &
This command discards both standard output and standard error by redirecting them to /dev/null, the null device. This is useful when you don't need to see the output of the command. The process still runs in the background due to the trailing &.

Checking the status of a nohup process

ps aux | grep my_script.sh
After running a command with nohup, you can use ps aux to list all processes and then filter with grep to find your specific process (my_script.sh in this case). This allows you to monitor its status and, if necessary, kill it.

Commonly used flags

Flag Description Example
--help Displays help information and exits. nohup --help
--version Displays version information and exits. nohup --version
(None) Without specific flags, nohup simply runs the given command while ignoring the SIGHUP signal. nohup ./long_running_process.sh


Share on Share on

Comments