How to use the nohup command in Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. In this post, you will learn how to use the nohup command on Linux. Let’s get started.

Introduction

In general, the nohup command allows you to run a command immune to hangups, with output to a non-type.

What does this mean? It allows you to keep the execution of a command independent of the terminal session. Making it continue to run despite closing the terminal.

As you can notice, the nohup command is quite useful in situations of configurations that we want to be done in the background and without affecting the user’s work.

Being a bit more technical, the nohup command ignores the HUP signal, which is the one sent to the process when the controlling terminal is closed, making it still alive.

The operation is simple, but it can be really useful.

Using the nohup on Linux command

In short, the syntax of the nohup command is as follows

nohup COMMAND [ARGS]

The nohup command is so basic that it only accepts --help and --version as parameters.

So, the operation of it is tied to the command you actually want it to run.

For example:

nohup touch
Using the nohup command
Using the nohup command

In this case, you know that the touch command will not run properly. But nohup will still redirect the output of the command to a file called nohup.out in the current directory at the prompt.

In case there are no write permissions on the desktop, the file will be created in the HOME directory.

The interesting thing is that if you query the contents of the file

cat nohup.out
Using the command
Using the command

You will see the output returned by the touch command, which is nothing more than an error because it is not used that way.

However, the real utility of the nohup command lies in the fact that it can be used in the background. This allows you to interact with the terminal without affecting the command.

nohup [command] &

This will generate a PID, but you will still be able to use the terminal. In addition to this, the output will still be redirected to the file mentioned above.

You can also redirect the command’s output to another file

nohup [command] > [command].out 2>&1 &

The above command redirects both standard output and errors.

But if you want them to be in different files, then you can run

nohup [command] > [command].out 2> [command].err &

As you can see, it is simple.

Conclusion

nohup is an excellent command for working within professional environments and configurations. Today, you learned how to use it, at least in a basic way.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook