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

terminal-multiple-monitors

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.

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.

bash-background-command-&

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.

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.

bash-background-disown-jobs

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

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.

bash-background-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.

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.

bash-background-no-hup

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.

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!

Image Credit: One terminal–four screens

8 comments

  1. 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.

  2. 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.

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

    sometask & someothertask & wait

  4. 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.

  5. What is the best way to launch something in the background and then use a semi-colon to run a second command? Bash really hates it when you do something like:

    md5sum hugefile &; top -c

Comments are closed.

Sponsored Stories