Linux: Run Process in the Background and Detach
Using disown
So let’s start a job in the shell.
firefox &
This will start the job in the background of the shell, but it will still be a child of the terminal. We can confirm by running jobs
.
If we want to keep the application running after we close or disconnect from the shell, then we need to use disown
. If we have multiple jobs, then we can also use %n
firefox &
disown
firefox &
code &
disown %1
Putting it all together while ignoring stdout and stderr in the terminal
firefox >/dev/null 2>&1 & disown
Yes the disown applies to the same command and not any currently active job
nohup
nohup
, is an application that executes another application such that it won’t receive a SIGHUP
when the shell receives the hang up signal. Typically, we also need to redirect stdout
and stderr
if we don’t want the creation of a nohup.out
file which is the default behaviour.
nohup firefox >/dev/null 2>&1 &
In this command, we are informing the shell, to redirect output to /dev/null
(a place that discards everything that is written to it, and returns an End of File - EOF if read from). We also inform the shell to redirect the stderr (2) to wherever the stdout (1) is directed (&) to.
Using dex
for Desktop Entries
Another way is to use dex
to run the “Exec” value of a .desktop
file if the entry type is Application
.
dex /usr/share/applications/firefox.desktop
Subshell
Run the command (with bg) in a sub-shell like so
(firefox &)
To ignore output,
(firefox >/dev/null 2>&1 &)