How do I start multiple programs in the background, with subshells, and keep them attached?

58 Views Asked by At

I have a number of programs, each in their own subdirectory that I need to run in the background.

  • They all need to be started with their own directory as the working directory.
  • They all print to stdout that I would like to see merged in the terminal.

I have the following script, but it doesn't work:

#/bin/bash

(cd service1dir; service1) &
(cd service2dir; service2) &
(cd service3dir; service3) &
(cd service4dir; service4) &

Only one program gets started, but I don't know why.

FYI, target platform is git-bash on Windows, but I want it to work on macOS Sierra as well.

1

There are 1 best solutions below

0
On

I think you don't need those parentheses. But then need to cd back into the root dir of that scripts.

#!/bin/bash

cd service1dir; service1 &
cd -;
cd service2dir; service2 &
cd -;
cd service3dir; service3 &
cd -;
cd service4dir; service4 &

This works for me under linux bash. All services are started in background while output goes to stdout.