Linux nohup Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux nohup command to run long-running processes in the background, even after you log out of your terminal session. You will understand the purpose of the nohup command, run a long-running process using nohup, and redirect the output to a file. This lab covers practical examples of process management in a Linux environment.

The nohup command allows you to run a process that will continue to execute even after you log out, preventing the process from being terminated. You will start by running a simple command using nohup and verifying that the process is still running. Then, you will create a script that runs a long-running process and use nohup to run it in the background, with the output saved to a file.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") subgraph Lab Skills linux/sleep -.-> lab-422844{{"`Linux nohup Command with Practical Examples`"}} linux/redirect -.-> lab-422844{{"`Linux nohup Command with Practical Examples`"}} linux/grep -.-> lab-422844{{"`Linux nohup Command with Practical Examples`"}} linux/ps -.-> lab-422844{{"`Linux nohup Command with Practical Examples`"}} end

Understand the Purpose of the nohup Command

In this step, you will learn about the purpose of the nohup command in Linux. The nohup command is used to run a command or script in the background, even after you log out of your terminal session.

When you run a long-running process in a terminal, it will be terminated if you log out or close the terminal window. The nohup command allows you to run a process that will continue to run even after you log out, preventing it from being terminated.

Let's start by running a simple command using nohup:

nohup sleep 60 &

This will run the sleep 60 command in the background, and the output will be saved to a file named nohup.out in the current directory.

Example output:

[1] 12345

The & at the end of the command runs the process in the background, and the output shows the process ID (PID) of the background process.

To verify that the process is still running, you can use the ps command:

ps aux | grep sleep

Example output:

labex     12345  0.0  0.0   8184   712 pts/0    S    14:20   0:00 sleep 60

As you can see, the sleep 60 process is still running, even though we've logged out of the terminal.

Run a Long-Running Process Using nohup

In this step, you will learn how to use the nohup command to run a long-running process in the background.

Let's start by creating a simple script that runs a long-running process. Create a new file named long_process.sh in the ~/project directory with the following content:

#!/bin/bash
echo "Starting long-running process..."
sleep 120
echo "Long-running process completed."

Make the script executable:

chmod +x ~/project/long_process.sh

Now, let's run the script using nohup:

nohup ~/project/long_process.sh &

This will start the long-running process in the background, and the output will be saved to a file named nohup.out in the current directory.

Example output:

[1] 12345

The output shows the process ID (PID) of the background process.

To verify that the process is still running, you can use the ps command:

ps aux | grep long_process.sh

Example output:

labex     12345  0.0  0.0   8184   712 pts/0    S    14:20   0:00 /bin/bash /home/labex/project/long_process.sh

As you can see, the long-running process is still running, even though we've logged out of the terminal.

Redirect nohup Output to a File

In this step, you will learn how to redirect the output of a long-running process started with nohup to a custom file instead of the default nohup.out file.

Let's start by running a long-running process and redirecting its output to a custom file:

nohup ~/project/long_process.sh > ~/project/output.log 2>&1 &

Here's what each part of the command does:

  • nohup ~/project/long_process.sh: Runs the long_process.sh script using nohup.
  • > ~/project/output.log: Redirects the standard output (stdout) of the process to the output.log file in the ~/project directory.
  • 2>&1: Redirects the standard error (stderr) of the process to the same file as the standard output.
  • &: Runs the process in the background.

To verify that the process is still running and the output is being redirected to the custom file, you can use the following commands:

ps aux | grep long_process.sh
cat ~/project/output.log

The ps command will show that the long-running process is still running, and the cat command will display the contents of the output.log file.

Summary

In this lab, you learned about the purpose of the nohup command in Linux, which allows you to run a long-running process in the background, even after you log out of your terminal session. You started by running a simple sleep 60 command using nohup, and then verified that the process was still running using the ps command. Next, you created a script that runs a long-running process and used nohup to run it in the background, with the output being saved to a file named nohup.out.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like