0

I'm playing with making a script for the first time and would like to loop through and start a few node.js apps. Every time I start the first app though the loop breaks.

I've searched for some time to get a solution and looked into the -n flag used for ssh, </dev/null and using (command) to create a new terminal for commands. Perhaps the answer is in these but I'm not seeing it.

Any help is much appreciated. Thanks!

for A in "${ARR[@]}"
do
    case $A in
        $APP1)
            DIR=$APP1_DIR; FILE=$APP1_FILE;
            ;;
        $APP2)
            DIR=$APP2_DIR; FILE=$APP2_FILE;
            ;;
        *)
            printf "KABLAMO!"
            ;;
    esac

    if [ -d $DIR ]; then
        ( cd $DIR && node $FILE );

        # get nothing after here on first iteration and script hangs at this point

    fi
done
2
  • What do the node.js scripts do? This will wait for the script to finish before continuing the loop. If you don't want to wait, run it in the background. Commented Oct 2, 2017 at 17:55
  • Oops, some typos when modifying for question, thanks for pointing out @Barmar The node.js scripts are servers that are always running and listening on ports, so they never finish really, this is the problem? Commented Oct 2, 2017 at 17:58

1 Answer 1

1

Since the scripts don't exit, you need to continue without waiting for them. So you should run them in the background with &:

(cd "$DIR" && node "$file" &)
Sign up to request clarification or add additional context in comments.

1 Comment

You hero! Thanks a lot! A single &, wish it was a lot more convoluted! :P Been Googling and staring at this for hours! Thanks so much for the lightning fast response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.