[email protected]Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMay 2, 2023

How to use nohup command.

How To Use Nhup Command.

The nohup command executes another program specified as its argument and ignores all SIGHUP (hangup) signals. SIGHUP is a signal that is sent to a process when its controlling terminal is closed.

Usually, when you run a program over SSH, if your connection drops or you log out, the session is terminated, and all the processes executed from the terminal will stop. This is where the nohup command comes in handy. It ignores all hangup signals, and the process will continue to run.

 

How to Use the nohup Command

The syntax for the nohup command is as follows:

nohup COMMAND [ARGS]

The command doesn’t accept any other options except the standard –help and –version.

Let’s take a look at the following example:

nohup mycommand
nohup: ignoring input and appending output to 'nohup.out'

nohup runs the mycommand command in the foreground and redirects the command output to the nohup.out file. This file is created in the current working directory . If the user running the command doesn’t have write permissions to the working directory, the file is created in the user’s home directory.

If you log out or close the terminal, the process is not terminated.

 

Running the Command in Background

Using nohup in the foreground is not very useful because you won’t be able to interact with the shell until the command completes.

To run the command in the background, append the & symbol at the end of the command:

nohup mycommand &

The output includes the shell job ID (surrounded with brackets) and process ID:

[1] 25177

You can use the job ID to bring the command into the foreground using the fg command.

If for some reason you want to terminate the process, use the kill command followed by the process ID:

kill -9 25132

 

Redirecting Output to a File

By default, nohup redirects the command output to the nohup.out file. If you want to redirect the output to a different file, use the standard shell redirection.

For example, to redirect the standard output and standard error to the mycommand.out you would use:

nohup mycommand > mycommand.out 2>&1 &

To redirect the standard output and standard error to different files:

nohup mycommand > mycommand.out 2> mycommand.err &

 

Alternatives

There are several alternative programs that you can use to avoid a command to be terminated when you close the terminal or get disconnected.

 

Screen

Screen or GNU Screen is a terminal multiplexer program that allows you to start a screen session and open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected.

 

Tmux

Tmux is a modern alternative to the GNU screen. With Tmux, you can also create a session and open multiple windows inside that session. Tmux sessions are persistent, which means that programs running in Tmux will continue to run even if you close the terminal.

 

Disown

disown is a shell builtin that removes a shell job from the shell’s job control. Unlike nohup, you can use disown on running processes too.

 

Conclusion

nohup allows you to prevent commands from being terminated when you log out or exit the terminal.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Mon – Fri 9:00AM – 5:00PM