webstudent webstudent - 2 years ago 157
Bash Question

Running "node app.js" in loop breaks after first iteration

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

Answer Source

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" &)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download