Skip to content

nohup Linux Command Guide

The Linux nohup command is used to run commands that prevent them from being terminated even when exiting the session. It stands for “no hang up” and is often used to run processes that need to continue running in the background. This command is particularly useful when running long-running tasks or scripts remotely over an SSH connection. By using nohup, you can ensure that your command will keep running even if the connection is lost or the session is ended.

nohup Syntax:

Terminal window
nohup [option] [command] &

Options:

OptionDescription
-nIgnore the hangup signal
-cReuse the same standard input
-fIgnore access to the job control
-pPreserve existing processes
-helpDisplay help for nohup command

Parameters:

ParameterDescription
commandThe command to execute with nohup

nohup Command Samples:

Run a Command in the Background

Terminal window
nohup ./my_script.sh &

Runs the script “my_script.sh” in the background using nohup.

Redirect Output to a File

Terminal window
nohup ./my_command.sh > output.log &

Executes “my_command.sh” using nohup and redirects the output to a file named “output.log”.

Continue Running a Process After Logout

Terminal window
nohup ./my_long_process.sh &

Starts a long-running process “my_long_process.sh” using nohup so that it continues running even after logout.

Combine Standard Output and Error

Terminal window
nohup ./my_program 2>&1 > output.log &

Runs “my_program” using nohup, combining standard output and error messages and storing them in “output.log”.

Monitor System Metrics

Terminal window
nohup top -d 10 -b > top_metrics.log &

Starts monitoring system metrics using the “top” command with a 10-second delay, running in the background with nohup and outputting the results to “top_metrics.log”.

Execute a Command with Specific Arguments

Terminal window
nohup python my_script.py arg1 arg2 &

Runs a Python script “my_script.py” with arguments “arg1” and “arg2” using nohup to keep it running in the background.

Schedule a Task for Delayed Execution

Terminal window
nohup sh -c 'sleep 3600 && ./my_delayed_task.sh' &

Schedules the execution of “my_delayed_task.sh” after a delay of 3600 seconds (1 hour) by combining “sleep” and nohup commands.

How can I run a command in the background using nohup?

To run a command in the background using nohup, use the following syntax:

Terminal window
nohup <command> &

How can I save the output of a command run with nohup to a file?

To save the output of a command run with nohup to a file, you can use redirection as follows:

Terminal window
nohup <command> > output.log 2>&1 &

How do I check the status of a background process started with nohup?

To check the status of a background process started with nohup, you can use the ps command with options to display all processes:

Terminal window
ps -ef | grep <process_name>

How can I stop or terminate a process started with nohup?

To stop or terminate a process started with nohup, you can use the kill command followed by the process ID (PID):

Terminal window
kill <PID>

Can I run multiple commands in the background with nohup?

Yes, you can run multiple commands in the background with nohup by separating them with semicolons. For example:

Terminal window
nohup <command1> ; <command2> &

How can I run a nohup command on a remote server and disconnect without stopping the process?

To run a nohup command on a remote server and disconnect without stopping the process, you can use SSH along with nohup. For example:

Terminal window
ssh user@remote_server 'nohup command > output.log 2>&1 &'

Applications of the nohup command

  • Running a command in the background without being affected by hangups
  • Keeping a process running even after the user logs out
  • Preventing a process from being terminated when the terminal is closed