How do I run multiple commands together in the background?

8,104

(command1; command2)& - should do it, works in bash.

This creates a subshell (the two parenthesis) and runs the whole subshell in the background.

Share:
8,104

Related videos on Youtube

Trevor Hickey
Author by

Trevor Hickey

Updated on September 18, 2022

Comments

  • Trevor Hickey
    Trevor Hickey over 1 year

    I know I can run a program in the background using &.

    command &
    

    However, I want to run multiple commands, and cd into a different directory while they are running.
    The multiple commands will still rely on the directory I was previously in.

    I've tried the following, but it only runs the last command in the background:

    command1 && command2 &
    

    Doing this gives a parse error:

    command1 & && command2 & 
    

    It's important that command1 finishes before command2,
    so I don't think the following would guarantee that:

     command1 &; command2 &;  
    

    I'm not tied to any specific shell.

    • Admin
      Admin about 8 years
      @choroba Not just not needed, it's a syntax error (in bash and most other shells): there needs to be a command before ;
    • Admin
      Admin almost 4 years
      If you need to do that in a bash script, use the method suggested here: stackoverflow.com/a/62832235/1423806