
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.
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.& 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
&
.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.
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
.
command &>/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.nohup command &>/dev/null &

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.
Aucun commentaire:
Enregistrer un commentaire