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. 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: 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.) 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.
& 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, 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. 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.