How to Keep Processes Running Using Linux Command nohup

Linux Command nohup to Keep Processes Running

nohup command in Linux allows processes to continue running even after the user logs out or the terminal session ends.

It does this by redirecting standard output and error to a specified file (defaulting to “nohup.out”) or to the null device (if not specified).

This prevents the process from being terminated when the session ends, making it useful for long-running tasks or processes that need to run independently of the user’s active session.

  • Linux Command nohup Syntax:
nohup command [arg1 arg2 ...] [options]

Prerequisites to Use Linux nohup Command

Provide the options below to let this tutorial work correctly and move on.

  • A Linux VPS running a compatible Linux distribution (e.g., Ubuntu, Debian, CentOS).
  • A non-root user with sudo privileges.
  • Access to Terminal/Command line.

Practical Examples to Understand Linux Command nohup

For a reliable environment to test these nohup examples, consider buying Linux VPS. Then, go through the examples in this guide to learn How to Use Linux Command nohup.

1. Running a Process with nohup

This example runs a command with nohup. The output is appended to the nohup.out file.

  • Syntax:
nohup command
  • Example:
nohup ping google.com

This command runs a continuous ping to Google’s server using nohup, and the output is stored in nohup.out. You can check the output with:

cat nohup.out

1. Running a Process in Background

In Linux, running a process in the background allows it to continue executing independently of the terminal or user session.

Linux nohup command runs a command in the background that will not terminate even after logging out.

  • Syntax:
nohup command &
  • Example:
nohup ls -l &

This command runs ls -l in the background.

2. Redirecting Output to a Specific File

In this example, nohup Linux command saves both standard output and standard error into a specified log file.

  • Syntax:
nohup command > output_file 2>&1 &
  • Example:
nohup ./opera_script.sh > script.log 2>&1 &

This command redirects the output of opera_script.sh to script.log.

3. Running a Python Script with nohup

Using Linux nohup command is useful for running Python scripts that take a long time to execute.

  • Syntax:
nohup python3 script.py > output.log 2>&1 &
  • Example:
nohup python3 data_processing.py > process.log 2>&1 &

The data_processing.py script will run in the background, and the output will be saved to process.log.

4. Running a Shell Script with nohup

Ensuring long-running shell scripts are not terminated after logout is another usage of the Linux nohup command.

  • Syntax:
nohup ./script.sh > output_file 2>&1 &
  • Example:
nohup ./backup.sh > backup.log 2>&1 &

This command runs the backup.sh script and logs its output to backup.log.

5. Running a Process Without Redirecting Output

Using this example, you can discard the output if you don’t care about saving it.

  • Syntax:
nohup command > /dev/null 2>&1 &
  • Example:
nohup ./operaserver.sh > /dev/null 2>&1 &

Here, output and errors from operaserver.sh are discarded.

6. Viewing the nohup Output in Real-Time

You can use tail command to view the output file generated by nohup in real-time.

  • Syntax:
tail -f output_file
  • Example:
tail -f nohup.out

This command displays the ongoing output from the nohup command in nohup.out.

7. Running Multiple Commands with nohup

You can use the nohup command to run multiple commands in sequence to ensure they continue running.

  • Syntax:
nohup sh -c 'command1 && command2' > output_file 2>&1 &
  • Example:
nohup sh -c './task1.sh && ./task2.sh' > tasks.log 2>&1 &

This command runs task1.sh followed by task2.sh and logs the output to tasks.log.

8. Running Commands in the Background with Custom Priority

You can use nohup with nice to adjust the process priority.

  • Syntax:
nohup nice -n priority command > output_file 2>&1 &
  • Example:
nohup nice -n 10 ./heavy_task.sh > heavy_task.log 2>&1 &

This command runs heavy_task.sh with a lower priority and logs output to heavy_task.log.

9. Using nohup with SCP for File Transfer

Using nohup command, you can ensure file transfers via SCP continue even if the session is disconnected.

  • Syntax:
nohup scp source_file user@remote_host:/destination_path > scp.log 2>&1 &
  • Example:
nohup scp file.txt [email protected]:/home/user/ > scp_transfer.log 2>&1 &

This command transfers file.txt to a remote server, and the output is saved to scp_transfer.log.

10. Running a Command Periodically Using nohup and watch

This example explains the way you can run a command at regular intervals in the background using watch with nohup.

  • Syntax:
nohup watch -n interval command > output_file 2>&1 &
  • Example:
nohup watch -n 60 ./check_status.sh > status.log 2>&1 &

This command runs check_status.sh every 60 seconds and logs the output to status.log.

How to Use Linux Command nohup to Kill a process?

While the nohup command is often used to run long-running processes in the background, you can use it to terminate or kill these processes.

Since the process runs independently of the terminal, you can not just use the usual Ctrl + C to stop it. Instead, you will need to identify the process and terminate it using the kill command.

To Kill a Process Started with nohup, follow the below steps:

  1. Find the process ID (PID) of the process you want to kill. You can do this using the ps command or pgrep to search for the process by its name.
  2. Once you have the PID, use the kill command to terminate it.
  • Syntax:

Use the command below to find the process:

ps aux | grep command_name

Or

pgrep command_name

To Kill the process, run:

kill PID

Note: If the process doesn’t stop with a simple kill command, use the -9 flag to forcefully terminate it:

kill -9 PID
  • Example:

Suppose you started a long-running Python script using nohup:

nohup python3 my_script.py > output.log 2>&1 &

To kill this process, find the PID using the command below:

ps aux | grep my_script.py

This will output something like:

user     12345  0.5  2.0 python3 opera_script.py

To kill the process, use the kill command with the PID:

kill 12345

If the process doesn’t stop, you can force it to stop with:

kill -9 12345

What is nohup Command and why should I use it?

The Linux nohup command (short for “no hangup”) allows you to run a command in the background, ensuring that it continues running even if the terminal is closed or you log out of your session.

It’s useful for long-running tasks like backups, data processing, or server scripts.

How does nohup work?

When you use nohup, it redirects the standard output and error of the process to a file named nohup.out or to the null device (/dev/null) if specified.

This prevents the process from being terminated when the terminal session ends.

When should I use nohup command in Linux?

You should consider using nohup for tasks that:

  • Are long-running and don’t require immediate interaction.
  • Need to continue running even after you log out.
  • Produce a lot of output that you want to save.

Where does the output of a nohup command go?

By default, the output of a command run with nohup is redirected to a file called nohup.out in the directory where the command was executed.

If the directory is not writable, it will be stored in your home directory (~/nohup.out).

Also, you can explicitly specify an output file to capture both stdout and stderr:

nohup command > output.log 2>&1 &

Why am I not seeing the nohup.out file?

If the command you ran does not produce any output, or if you’ve redirected the output to another file, you may not see a nohup.out file.

To ensure the output goes to a specific file, redirect the output when running the nohup command:

nohup command > my_output.log 2>&1 &

Why does nohup still terminate my process when I log out?

If the process is terminated after logging out, it could be due to other factors like SSH session hangup or the command not being properly detached.

Make sure the command is fully backgrounded by adding & at the end:

nohup command > output.log 2>&1 &

Additionally, if the process is still tied to the terminal, use disown after running the process:

disown

Why is my nohup command giving a “Permission denied” error?

A “Permission denied” error usually indicates that the user running the command doesn’t have the necessary permissions to access the file or directory, or execute the command.

To troubleshoot it, ensure that you have execute permissions on the script or command:

chmod +x script.sh

If you’re redirecting output, make sure you have write permissions to the file or directory:

nohup ./script.sh > /path/to/output.log 2>&1 &

How to combine nohup with cron or other scheduling tools?

While cron already runs commands in the background, combining nohup with cron is redundant. Instead, you can specify the output redirection in the cron job without needing nohup.

Example in cron:

0 2 * * * /path/to/script.sh > /path/to/output.log 2>&1

Why is nohup.out empty?

If the nohup.out file is empty, it might be because the process didn’t produce any output or because the output was redirected elsewhere.

Check the process’s configuration or logs for more information.

Why is my nohup process not running?

There could be several reasons for this:

  • The process may have crashed or been terminated.
  • There might be permission issues preventing the process from running.
  • The process might be waiting for resources that are not available.
  • Check the system logs for any error messages related to the process.

Conclusion

Throughout this guide, we explored practical examples of using nohup to run commands in the background, redirect output to files, manage multiple tasks, and ensure uninterrupted execution.

Whether you’re running Python scripts, transferring files, or handling complex tasks, nohup offers a simple yet effective way to keep your processes running independently of your terminal.

The Linux nohup command is ideal for long-running tasks, such as backups, scripts, or data processing.

tmux is another terminal multiplexer that allows you to create, detach, and reattach terminal sessions.

Leave a Reply

Your email address will not be published. Required fields are marked.