Linux command with nohup

nohup Command in Linux with Examples

Every command in Linux starts a process at the time of its execution, which automatically gets terminated upon exiting the terminal. Suppose, you are executing programs over SSH and if the connection drops, the session will be terminated, all the executed processes will stop, and you may face a huge accidental crisis. In such cases, running commands in the background can be very helpful to the user and this is where nohup command comes into the picture. nohup (No Hang Up) is a command in Linux systems that runs the process even after logging out from the shell/terminal.

Usually, every process in Linux systems is sent a SIGHUP (Signal Hang UP) which is responsible for terminating the process after closing/exiting the terminal. Nohup command prevents the process from receiving this signal upon closing or exiting the terminal/shell. Once a job is started or executed using the nohup command, stdin will not be available to the user and nohup.out file is used as the default file for stdout and stderr. If the output of the nohup command is redirected to some other file, nohup.out file is not generated.

nohup command [command-argument . ]

Working with nohup command

1. Checking the version of Nohup:

The version of nohup command can be checked by using the following command.

2. To run a command in the foreground:

Runs the command in the foreground and redirects the command output to the nohup.out file if any redirecting filename is not mentioned.

To redirect the output to the output.txt file:

$ nohup bash geekfile.sh > output.txt

3. To run a command in the background (with ‘&’):

To run the command in the background, the ‘&’ symbol is appended at the end of the command. After executing, it doesn’t return to the shell command prompt after running the command in the background. It can be brought back to the foreground with the fg command.

$ nohup bash geekfile.sh & fg

Note: The number within square brackets represents the job id and the number next to it is the process id.

Читайте также:  Linux перезагрузка при kernel panic

4. To run multiple commands in the background:

nohup command can be used to run multiple commands in the background.

To run multiple commands in the background:

Here, the output will be by default stored in nohup.out. To redirect it, type:

$ nohup bash -c 'commands' > filename.txt
$ nohup bash -c 'cal && ls' > output.txt

Источник

Nohup Command in Linux Enables You to Run Commands Even After Logging Out

This article will show you how you can start a process and keep it running even after you have logged out using the nohup command.

Nohup stands for ‘no hang up’. It’s an extremely useful command to make your process running even after you log out.

One of the most common use can be found in running time-taking commands over SSH connection. If you think that your SSH session may drop, you can use the command with nohup in this fashion:

What is nohup command?

Nohup can be seen as a wrapper. You prefix your command with nohup like this:

Nohup‘s purpose is to intercept and prevent SIGHUP signals from reaching COMMAND.

When you prefix your command with nohup, the input will be redirected from /dev/null ( nohup COMMAND < /dev/null ). In simple word, don’t call a command with nohup if you need to send input to that command. When you nohup a command, it needs to be able to run unattended.

Nohup will first attempt to redirect the output into ./nohup.out or will send it to ~/nohup.out if need be. You can also decide where to redirect the output like this:

nohup COMMANDS >/path/to/output/file

There is not much to say about nohup, it does not have a long list of options. There is only –help and –version.

Example of using nohup command

Executing a script with nohup is as simple as this.

nohup command

Note that nohup did not fork the process to the background. Nohup is only there to intercept SIGHUP signals. Once the command is running, you can proceed and close your terminal. It will warn you that a process is running. Confirm you want to kill it.

nohup command while logging out of the session

When you closed your terminal, all child processes received a SIGHUP signal. Nohup prevented that signal from reaching my command. The following picture shows process information about my command while the terminal was still running.

You see the PPID? That is the parent process ID. When you requested to close the terminal, all children of PPID received a SIGHUP signal. Nohup prevented that signal from reaching our script. The parent process terminated and our script got orphaned. When a process gets orphaned, it automatically gets assign 1 (systemd or init) as a parent.

Читайте также:  Rfr epyfnm dthcb linux

As you can see it is really not hard to demonstrate the use of nohup. Very simple command. You can fork the process in the background like this:

You can also decide where the output is redirected to like so:

Note that if you shutdown or reboot Linux system, it will kill the command. When you request a shutdown or reboot, all processes receives a SIGTERM and then a SIGKILL. Those signals are not intercepted by nohup.

Nohup through SSH connection

One use that often comes to mind right away when you learn nohup is to be able to start a process on a remote computer through SSH. Logically, running a command with nohup would allow you to logoff from your SSH connection and the process would still run.

One common problem is that sometimes SSH will “hang” while logging off. This is because SSH will often refuse to lose any data stream to and from any background processes. It will then “hang” until the process is terminated.

This issue can often be dealt with by redirecting all 3 data stream.

nohup COMMAND >./nohup.out 2>./nohup.err &

I hope this quick article helped you to better use SSH sessions. If you have questions or suggestions, please leave a comment below.

Источник

Bash nohup command tutorial

The meaning of nohup is ‘no hangup‘. Normally, when we log out from the system then all the running programs or processes are hangup or terminated. If you want to run any program after log out or exit from Linux operating system then you have to use nohup command. There are many programs that require many hours to complete. We don’t need to log in for long times to complete the task of the command. We can keep these type of programs running in the background by using nohup command and check the output later. Some example of using nohup command are memory check, server restart, synchronization etc. How you can use a nohup command on Ubuntu to run a program in the background is shown in this tutorial.

You can check the version of nohup command by using the following command.

nohup command syntax:

You can use the nohup command by two ways.

Using nohup with commands

Example-1: Using nohup command without ‘&’

When you run nohup command without ‘&’ then it returns to shell command prompt immediately after running that particular command in the background. In the following example, nohup run bash command without ‘&’ to execute sleep1.sh file in the background. The output of the nohup command will write in nohup.out the file if any redirecting filename is not mentioned in nohup command. For the following command, you can check the output of sleep1.sh by checking the output of nohup.out file.

Читайте также:  Посмотреть открытые порты linux команда

You can execute the command in the following way to redirect the output to the output.txt file. Check the output of output.txt.

Example-2: Using nohup command with ‘&’

When nohup command use with ‘&’ then it doesn’t return to shell command prompt after running the command in the background. But if you want you can return to shell command prompt by typing ‘fg’

Example-3: Using nohup command to run multiple commands in the background

You can run multiple commands in the background by using nohup command. In the following command, mkdir and ls command are executed in the background by using nohup and bash commands. You can get the output of the commands by checking output.txt file.

Example-4: Start any process in the background by using nohup

When any process starts and the user closes the terminal before completing the task of the running process then the process stops normally. If the run the process with nohup then it will able to run the process in the background without any issue. For example, if you run the ping command normally then it will terminate the process when you close the terminal.

You can check the list of all running command by using pgrep command. Close the terminal. Re-open the terminal and run pgrep command with -a option. No list of running command will display because all running commands are terminated when the terminal was closed.

Run ping command with nohup command. Re-open the terminal and run pgrep command again. You will get the list of the process with process id which is running.

You can stop any background process by running kill command. Just run kill command with particular process id which is running. Here, the process id of the running process is 7015. Run kill command in 7015 to terminate the process.

The uses of nohup command are explained by using very simple examples in this tutorial. Hope you will get a clear idea of the function of nohup command and able to apply this command for various purposes.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Оцените статью
Adblock
detector