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.

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

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

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

& 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

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.

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.

Screenshot From 2021 01 14 16 32 43

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.

Related:

John Perkins 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.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.