How to Run Bash Commands in the Background in Linux

Background Commands Feature

There’s nothing more annoying than running a command in your terminal and having it run for minutes, sometimes hours, and not be able to use your terminal again. Sure, you can use tabs, but that’s a clunky solution, and it’s not always optimal because you may want to see updates as you’re working. Here we show you a few different ways to run bash commands in the background in Linux.

Also read: How to Use lsof Command in Linux to List Open Files

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. This way, you can issue a command in the background and continue to use your terminal as it runs. It comes with a catch, though. Using & doesn’t disconnect the command away from you; it just pushes it into the background. This means that while you’re trying to use the terminal, anything the command wants to push to STDOUT or STDERR will still be printed, which may be distracting.

Screenshot From 2021 01 14 16 21 23
COMMAND &

When the terminal session is closed, the command ends. You can also kill the command by issuing the jobs command, finding the number of the command that’s running, and killing it with the kill command. That syntax is as follows:

Screenshot From 2021 01 14 16 21 48
kill %1

Using & is good if you need to push something off for a bit but don’t expect it to continue forever.

Also read: How to Use the dd Command in Linux

& 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, start by adding an &.

COMMAND &

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 as we saw before.

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

Screenshot From 2021 01 14 16 26 01
You can just make out the disown command in there

Now you can close your terminal and continue about your day. It’ll still keep piping things to STDOUT or STDERR, but once you exit and reopen your terminal, you won’t see anything there. You can find the command again with the top or ps commands and kill it with the kill command.

Screenshot From 2021 01 14 16 28 42
The disowned job is the second one, with the PID 16238.

Also read: How to Use Rm Command in Linux

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

Screenshot From 2021 01 14 16 31 33
COMMAND &>/dev/null &

This does not prevent the command from closing when the terminal closes. However, as mentioned above, it’s possible to use disown to disown the running command away from the user. You can also kill it in either of the methods mentioned above if you don’t want it to run anymore.

Also read: The Ultimate Guide to Apt and Apt-Get Commands

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.

nohup COMMAND &>/dev/null &
Screenshot From 2021 01 14 16 32 43

Also read: How to Generate SSL Certificates on Linux Using OpenSSL

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 new features.

Luckily, commands like nohup or disown are still a reality and can 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.

If you enjoyed this Linux article, make sure to check out some of our other Linux content, like how to connect your Google account to GNOME Shell, the best Linux distros for windows users, and LS commands you need to know.

Also read: How to Use cURL for Command Line Data Transfer and More

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

John Perkins

John is a young technical professional with a passion for educating users on the best ways to use their technology. He holds technical certifications covering topics ranging from computer hardware to cybersecurity to Linux system administration.