Меню Рубрики

Command run in background linux

How to Start Linux Command in Background and Detach Process in Terminal

In this guide, we shall bring to light a simple yet important concept in process handling in a Linux system, that is how to completely detach a process from its controlling terminal.

When a process is associated with a terminal, two problems might occur:

  1. your controlling terminal is filled with so much output data and error/diagnostic messages.
  2. in the event that the terminal is closed, the process together with its child processes will be terminated.

To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background.

How to Start a Linux Process or Command in Background

If a process is already in execution, such as the tar command example below, simply press Ctrl+Z to stop it then enter the command bg to continue with its execution in the background as a job.

You can view all your background jobs by typing jobs . However, its stdin, stdout, stderr are still joined to the terminal.

Run Linux Command in Background

You can as well run a process directly from the background using the ampersand, & sign.

Start Linux Process in Background

Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.

Linux Process Running in Background Message

Keep Linux Processes Running After Exiting Terminal

We will use disown command, it is used after the a process has been launched and put in the background, it’s work is to remove a shell job from the shell’s active list jobs, therefore you will not use fg , bg commands on that particular job anymore.

In addition, when you close the controlling terminal, the job will not hang or send a SIGHUP to any child jobs.

Let’s take a look at the below example of using diswon bash built-in function.

Keep Linux Process Running After Closing Terminal

You can also use nohup command, which also enables a process to continue running in the background when a user exits a shell.

Читайте также:  Создать файловую систему ext4 linux

Put Linux Process in Background After Closing Shell

Detach a Linux Processes From Controlling Terminal

Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (GUI) applications such as firefox:

In Linux, /dev/null is a special device file which writes-off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to /dev/null.

As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the a controlling terminal, your process and child processes will be terminated.

Importantly, for any questions or remarks on the subject, reach us by using the comment form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Running Bash Commands in the Background the Right Way [Linux]

Everyone’s been there: you’re looking to run a terminal program and keep it running. The trouble is this program is old or doesn’t include a feature that allows it to run as a daemon in the background. Luckily, there are several ways to force programs to work in the background anyway.

Bash can do this all on its own, and extra programs will not need to be installed. This article will go over several ways that you can push terminal programs into the background and keep them there. Each method listed is good for its own special-use case.

End a Command with &

If you want to push a command into the background, using & at the end is an easy way to do that. It comes with a catch, though. Using & doesn’t disconnect the command away from you; it just pushes it in the background so you can continue using a terminal.

Читайте также:  Основные команды linux fedora

When the terminal session is closed, the command ends. Using & is good if you need to push something off for a bit, but don’t expect it to continue forever.

& After a Command, Then Disown It

Running a command with just & pushes it off to the back and keeps it running as long as the terminal window is open. If, however, you’re looking to keep this command running in constant, even with your terminal session ending, you can use the disown command.

To use this method, first start off adding an & .

As mentioned above, using & pushes this command into the background but doesn’t detach it from your user. You can verify this by typing jobs into the terminal. It’ll show the command running in the background.

Just type disown into the shell, and it’ll do just that (and you can once again verify this with the jobs command).

& After a Command with /dev/null

Adding & after a command will push a command into the background, but as a result the background command will continue to print messages into the terminal as you’re using it. If you’re looking to prevent this, consider redirecting the command to /dev/null .

This does not prevent the command from closing when the terminal closes. However, like mentioned above, it’s possible to use disown to disown the running command away from the user.

Nohup, with & and /dev/null

Unlike the previous commands, using nohup allows you to run a command in the background and keep it running. How? Nohup bypasses the HUP signal (signal hang up), making it possible to run commands in the background even when the terminal is off. Combine this command with redirection to /dev/null (to prevent nohup from making a nohup.out file), and everything goes to the background with one command.

Conclusion

Most terminal programs on Linux today have features built in to allow them to run in the background with little effort. Along with that, modern init systems (like systemd) can allow users to start programs like services at boot or whenever.

Still, some programs on Linux lack the ability to run as a daemon or integrate with modern init systems. This is a real inconvenience but is understandable, as not all developers have the skill or time to add in new features.

Читайте также:  Linux перезагрузить сервер команда

Luckily, commands like nohup or disown are still a reality and can still close the gap in moving programs like this to the background. They’re not perfect or fancy, but they get the job done when needed.

How do you move running terminal programs into the background? Tell us below!

Derrik Diener is a freelance technology blogger.

8 comments

The most common way that I run my own tasks in the background is with the nohup command > /dev/null 2>&1 &

I wrote a short bash script to run this and it will take command arguments too.

Finally I also created a simple bash alias to invoke the script and I keep my scripts and aliases convenient for moving to another system.

Just to let you know, one other option that is really good if you have to be able to monitor and possibly respond to a long running terminal session is “screen”. Your terminal session stays up and you can get back to it even if you lose your connection.

Don’t forget about wait command to propagate SIGTERM to all background tasks.

sometask & someothertask & wait

Let’s say you’ve run a command but forgot to put the ‘&’ on the end. You can still dump it into the background with CTRL-Z, followed by entering the ‘bg’ command. CTRL-Z suspends the process, and then ‘bg’ resumes it in the background. You can use the ‘jobs’ command to see it running in the background, as described in the article. If for some reason you want to pull it back to the foreground, just enter the ‘fg’ command. If you have more than one job running in the background, just give fg the job number. (e.g. ‘fg %2’ will foreground the second job in your jobs list.)

You can use CTRL-Z, bg, and fg to run multiple commands in the background and bring a selected job to the foreground when needed.

For zsh I use
gitk &!
the ! detaches the process from the terminal.

Источник

Adblock
detector