How to Use the nohup Command in Linux

Configurare noua (How To)

Situatie

The Linux nohup command lets important processes carry on running even when the terminal window that launched them is closed.

Solutie

We created a program that doesn’t do anything useful, but it will run and run until it is terminated. It prints the time to the terminal window every three seconds. It’s called long-proc for “long process.”

./long-proc

The long-proc program running a terminal window

If this was a program that did something useful and we wanted it to continue to run even if the terminal window and shell are closed, we’d launch it with nohup.

nohup ./long-proc

launching the the long-proc program from nohup

The process is decoupled from stdin and stdout so it can neither receive any input nor write to the terminal window. Also, because it is still running, you’re not returned to the command prompt. All that nohup does is make the process impervious to the terminal closing down. It doesn’t turn the process into a background task.

Do you now have to reboot just to terminate the process? No. To stop a nohup process you haven’t launched as a background process, hit the Ctrl+C key combination.

Halting the long-proc process with Ctrl+C

The output from the program has been captured for us in a file called “nohup.out.” We can review it with less.

less nohup.out

Opening the nohup.out file in less

Anything that would usually be sent to the terminal window is captured in the file. Subsequent runs of nohup will be appended to the existing “nohup.out” file.

The output from long-proc written to the nohup.out file, displayed in less

A more useful way to run the process is to launch it with nohup so that it withstands the terminal window being closed, and to make it a background task at the same time. To do this we add an ampersand “&” to the end of the command line.

nohup ./long-proc &

launching the long-proc program with nohup and making it a background task

You’ll need to hit “Enter” once more to return to command prompt. We’re told the job number of the process is 1—the number in brackets “[]“— and that the process ID is 13115.

We can use either of these to terminate the process. “Ctrl+C” won’t work now because the program doesn’t have any association with either the terminal window or the shell.

If you forget what the job number is, you can use the jobs command to list the background tasks that have been launched from that terminal window.

jobs

Listing the background tasks that have been launched from a terminal window

To kill our task we can use the kill command and the job number, preceded by a percentage sign “%“, like this:

kill %1

If you’ve closed the terminal window you’ll need to find the process ID and use that with the kill command. The pgrep command will find the process ID for processes that match the search clue you provide. We’ll search for the process name.

pgrep long-proc

Finding the process ID of a process by name

Now we can use the process ID to terminate the process.

kill 13115

Using the kill command and process ID to terminate a process

The next time you hit “Enter” you’re informed that the process has been terminated.

Now let’s look at what doesn’t terminate the process. We’ll relaunch it, and then close the terminal window.

nohup ./long-proc

Closing the terminal window with a process running

If we open a new terminal window and search for our process with pgrep, we see it is still running. Closing the terminal window that launched the process has had no effect.

pgrep long-proc

Using pgrep to search for a process by name

It is possible to pass multiple commands to nohup, but it is usually better to launch them separately. It makes it easier to manipulate them as background jobs. The commands won’t run at the same time, they’ll be executed one after the other. The execution is not concurrent, it’s sequential. To have them run concurrently you need to launch them separately.

Having said that, to launch several processes at once, use nohup to launch a Bash shell and use the -c (commands) option with the string of commands. Use single quote marks “'” to wrap the command list and double ampersands “&&” to separate the commands.

nohup bash -c 'ls /bin && ls /sbin'

Launching two process with nohup

If you use less to look through the “nohup.out” file, you’ll see the output from the first process, then the output from the second process.

less nohup.out

Opening the nohup.out file in less

The output from both commands has been captured in the “nohup.out” file. It is not intertwined, the output from the second process only starts once the first process has terminated.

The contents of the nohup.out file in less

If you want to use a file of your own instead of “nohup.out”, you can redirect the command into the file of your choice.

nohup bash -c 'ls /bin && ls /sbin' > myfile.txt

redirecting the output from the processes to a user-provided file

Note that the message no longer says “appending output to nohupo.out”, it says “redirecting stderr to stdout” and we’re redirecting stdout to our “myfile.txt” file.

We can look inside “myfile.txt” file with less.

less myfile.txt

As before, it contains the output from both commands.

Tip solutie

Permanent

Voteaza

(8 din 12 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?