Sign In
Sign In

Nohup Command in Linux

Nohup Command in Linux
JC Brian Refugia
Technical writer
Linux
24.07.2024
Reading time: 4 min

In the Linux environment, controlling long-running processes and maintaining their continuity is critical, particularly for system administrators and developers. One of the most useful tools for this purpose is the nohup command. Short for "no hang up," nohup enables users to perform commands or scripts in the background, guaranteeing that they continue to execute even after logging out or losing connection. Understanding how to use nohup can dramatically improve a workflow, whether doing normal maintenance, running time-consuming scripts, or managing server activities.

Prerequisites

Make sure the following requirements are met before executing the nohup command in Linux to effectively manage long processes:

  • Access to a Linux terminal session where commands, such as nohup, can be executed.

  • To run commands, control processes, and explore directories, users must understand the fundamentals of Linux command line operations.

  • To perform commands and control processes on the system, make sure users have the necessary rights (particularly root or sudo access).

  • To monitor and control processes that were started with nohup command, it's helpful to have a basic understanding of the process management commands like ps and kill.

  • Make sure the machine has enough storage space available, particularly if the command produces output that is saved to a file.

How nohup Works

The nohup command in Linux allows process to continue running even after the user logs out or when the terminal session ends. Normally, when a user signs out or disconnected of a terminal session, all related processes receive a hangup signal (SIGHUP). This signal usually causes processes to end. However, when a process starts with nohup, the SIGHUP signal is ignored. By default, nohup redirects the command's standard output (stdout) and standard error (stderr) streams to a file called nohup.out in the current directory. This allows users to record any output or error messages created by the command even after the terminal session has terminated. When appending an ampersand (&) to the nohup command, it runs the specified command in the background.

Examples and Best Practices for Using nohup

The following are some examples of using the nohup command in Linux.

  • Navigate to the directory where the script is located (cd /home/testuser) and run the below command. This will execute the script in the background. 

Syntax: nohup ./script name &

Wherein: 

  • ./ indicates that the script will run in the current directory
  • script name is the name of the script to be run
nohup ./testscript.sh &

Image7

Press Enter. The output of the script will be directed to file called nohup.out

Image10

Validate it by running the command below.

ls -lrt

Image6

To check the logs generated by the script. View the content of the file named nohup.out by executing the command below.

cat nohup.out

Image4

  • To do the output redirection, execute the below command to redirect the output of the script to a file name.

Syntax: nohup ./script name > filename

nohup ./testscript.sh > testscript.out.log

Image8

View the content of the file named testscript.out.log by executing the command below.

cat testscript.out.log

Image2

  • Aside from running a script, user can also run command in background using nohup. To do this, run the command below and press Enter.

Syntax: nohup command &

nohup ps -ef &

Image9

View the content of the file nohup.out to see the output that has been generated by the command (ps -ef). Execute the command below.

cat nohup.out

Image3

  • Redirect the output of command using nohup. Run the command below.

Syntax: nohup command > outputfile

nohup ps -ef > ps.out.log

Image5

Then validate the output generated by running the command below.

cat ps.out.log

Image13

  • Chaining multiple commands with && allows nohup to execute them sequentially. See example below.

Syntax: nohup command1 && command2 && command3 &

nohup sleep 100 && echo “hello world” &

Terminating a Process Started by Nohup

Let’s say the command below was executed.

nohup sleep 100 && echo “hello world” &

Image11

  1. First, use ps to determine the process ID (PID). Execute the command below.

ps aux | grep sleep

Image12

We can see that PID is 104539.

  1. Kill the pid by running the command below.

kill 104539

Image1

Conclusion

In conclusion, the nohup command is an extremely useful tool for anyone working with Linux, especially system administrators and developers who need to manage long-running processes. By allowing commands to persist even after the user logs out, nohup ensures that key processes are completed uninterrupted, which is critical for system stability and productivity.

Linux
24.07.2024
Reading time: 4 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us