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:
Section titled “nohup Syntax:”nohup [option] [command] &
Options:
Section titled “Options:”Option | Description |
---|---|
-n | Ignore the hangup signal |
-c | Reuse the same standard input |
-f | Ignore access to the job control |
-p | Preserve existing processes |
-help | Display help for nohup command |
Parameters:
Section titled “Parameters:”Parameter | Description |
---|---|
command | The command to execute with nohup |
nohup Command Samples:
Section titled “nohup Command Samples:”Run a Command in the Background
Section titled “Run a Command in the Background”nohup ./my_script.sh &
Runs the script “my_script.sh” in the background using nohup.
Redirect Output to a File
Section titled “Redirect Output to a File”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
Section titled “Continue Running a Process After Logout”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
Section titled “Combine Standard Output and Error”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
Section titled “Monitor System Metrics”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
Section titled “Execute a Command with Specific Arguments”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
Section titled “Schedule a Task for Delayed Execution”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?
Section titled “How can I run a command in the background using nohup?”To run a command in the background using nohup, use the following syntax:
nohup <command> &
How can I save the output of a command run with nohup to a file?
Section titled “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:
nohup <command> > output.log 2>&1 &
How do I check the status of a background process started with nohup?
Section titled “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:
ps -ef | grep <process_name>
How can I stop or terminate a process started with nohup?
Section titled “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):
kill <PID>
Can I run multiple commands in the background with nohup?
Section titled “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:
nohup <command1> ; <command2> &
How can I run a nohup command on a remote server and disconnect without stopping the process?
Section titled “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:
ssh user@remote_server 'nohup command > output.log 2>&1 &'
Applications of the nohup command
Section titled “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