0

I'm trying to run multiple instances of a node server using a bash script. I parametrized node such that I could change the port at each cycle, but it seems I cannot run it properly. This is what I wrote (and doesn't start anything). It looks pretty ugly, I'm very new to bash scripting.

#!/bin/bash

#iterate input times and start server
COUNT=$1
TIMEOUT=$2
PORT=3000

while [ $COUNT -gt 0 ]; do
        A="node server.js "
        B=PORT
        C=" "
        D=TIMEOUT
        CMD=$A$B$C$D
        $CMD
        let PORT=PORT+10
        let COUNT=COUNT-1
done

TIMEOUT is just another variable that I pass to the server instance.

Can anyone point out what am I doing wrong? Thanks a lot, and sorry if the script looks ugly.

2 Answers 2

3
TIMEOUT=$2
for((count=$1,port=3000;count>0;--count,port+=10)); do
    node server.js $port $TIMEOUT &
done

You have way too many lost $-signs.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thanks a lot! That worked fine and looks way much cleaner than my ugly script. Thanks again!
1

You basically want to start a new thread, because otherwise the execution will wait for the process started with your command to exit. To do that, simply append ' & ' to the command, i.e.

...
$CMD &
...

Comments

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.