How do I run multiple background commands in bash in a single line?

LinuxBashShell

Linux Problem Overview


I normally run multiple commands with something like this:

sleep 2 && sleep 3

or

sleep 2 ; sleep 3

but what if I want to run them both in the background from one command line command?

sleep 2 & && sleep 3 &

doesn't work. And neither does replacing && with ;

Is there a way to do it?

Linux Solutions


Solution 1 - Linux

Exactly how do you want them to run? If you want them to be started in the background and run sequentially, you would do something like this:

{ sleep 2; sleep 3; } &

If you want sleep 3 to run only if sleep 2 succeeds, then:

sleep 2 && sleep 3 &

If, on the other hand, you would like them to run in parallel in the background, you can instead do this:

sleep 2 & sleep 3 &

And the two techniques could be combined, such as:

{ sleep 2; echo first finished; } & { sleep 3; echo second finished; } &

Bash being bash, there's often a multitude of different techniques to accomplish the same task, although sometimes with subtle differences between them.

Solution 2 - Linux

You need to add some parens in your last version --

(sleep 2 &) && (sleep 3 &)

or this also works --

(sleep 2 &) ; (sleep 3 &)

Solution 3 - Linux

to run multiple background command you need to add & end of each command. ex: (command1 &) && (command2 &) && (command3 &)

Solution 4 - Linux

The answers above use parentheses. Bash also can use braces for a similar purpose:

{ sleep 2 && sleep 3; } &

Note that the braces are more picky about syntax--the space after {, the space before }, and the final semicolon are required. In some situations the braces are more efficient because they don't fork a new subshell. In this case I don't know if it makes a difference.

Solution 5 - Linux

This works:

$(sleep 2 &) && sleep 3 &

Also you can do:

$(sleep 2 && sleep 3) &

Solution 6 - Linux

You can use the bash command substitution $(command) like this:

$(command1 ; command2) &

Note that stdin and stdout are still linked to the parent process and redirecting at least the stdout can be tricky. So alternatively you can chain the commands in a single line then pass the string to the bash command to spawn a new process which will handle the execution.

bash -c "command1 ; command2" & 

This is especially useful in a bash script when you need to run multiple commands in background.

This two statements should be equivalent. A bash process is spawn in both cases to handle the command (chain of commands) and the & at the end detaches the execution.

This time you can add &>/dev/null before the & at the end of the command to redirect at least the stdout and avoid the output on the stdout of the parent process. Something like:

bash -c "command1 ; command2" &>/dev/null &

Solution 7 - Linux

I have the same mission too. I have try (sleep2 ; fg )& sleep3 ; fg,it's working. And when you preass ctrl+c doubled,the two process can be stoppped.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPalace ChanView Question on Stackoverflow
Solution 1 - LinuxKitsuneView Answer on Stackoverflow
Solution 2 - LinuxiagreenView Answer on Stackoverflow
Solution 3 - LinuxsaeedView Answer on Stackoverflow
Solution 4 - LinuxGreg ReagleView Answer on Stackoverflow
Solution 5 - LinuxhiguaroView Answer on Stackoverflow
Solution 6 - LinuxBemipefeView Answer on Stackoverflow
Solution 7 - Linuxdingyang wangView Answer on Stackoverflow