Execute Linux commands in background and detach from terminal session

0
7588
Linux Operating System
Linux Operating System

How many ways do you know to execute a Linux commands in background and detach from terminal session? Below are several ways to achieve this task via command line.

Using screen utility to execute single or multiple commands.


# for single command
$ screen -dm COMMAND

# for multiple commands
$ screne -dm bash -c "COMMAND_1; COMMAND_2; ..."

The infamous nohup command.


$ nohup COMMAND &

Using disown.


$ COMMAND & disown

If you provide -h flag for disown, it will work like nohup.


$ COMMAND & disown -h

Using setsid.


$ setsid COMMAND >& /dev/null &

Using sub-shell.


$ (COMMAND >& /dev/null &)

Even better, you can try to combine them together. For example,


$ (nohup COMMAND >& /dev/null &)

$ (setsid COMMAND >& /dev/null &)

$ (setsid COMMAND >& /dev/null) & disown