I need to run a parallel for loop in posix shell (not bash). But it is currently not executing in docker and skipping that statement on adding &
to the for loop.
Reference question with details: Shell script not running as bash using Dockerfile
Code snippet from reference question:
if [ -z "$GWC_PC_ID" ]
then
ENV_NAME=$ENV_NAME RUN_OPTIONS=$SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT k6 run $K6_RUN_OPTIONS ''$SCENARIO/index.js''
else
for pcId in $(printf '%s' "$GWC_PC_ID" | tr , ' ');
do
ENV_NAME=$ENV_NAME RUN_OPTIONS=$SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT GWC_PC_ID=$pcId k6 run $K6_RUN_OPTIONS ''$SCENARIO/index.js'' &
done
fi
Here if I give empty input, it runs fine but for non-empty input, it does not execute statement inside for loop. Note that if I remove &
then it executes the statement inside for loop but I need to run those statements parallely.
CodePudding user response:
The issue was because it was not waiting for process to finish and exiting before it can execute the forked threads. For waiting for the forked threads, I needed an array of process ids. Since array is not supported I installed bash in my docker.
To do that I modified Dockerfile as follows:
FROM grafana/k6:0.40.0
COPY ./src/lib /lib
COPY ./src/scenarios /scenarios
COPY ./src/k6-run-all.sh /k6-run-all.sh
WORKDIR /
ENTRYPOINT []
USER root
RUN apk add --no-cache bash
CMD ["bash", "-c", "./k6-run-all.sh"]
Additionally I added the following to my script to wait for processes to finish:
if [ -z "$GWC_PC_ID" ]
then
ENV_NAME=$ENV_NAME RUN_OPTIONS=$SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT k6 run $K6_RUN_OPTIONS ''$SCENARIO/index.js''
else
i=0
for pcId in $(printf '%s' "$GWC_PC_ID" | tr , ' ');
do
ENV_NAME=$ENV_NAME RUN_OPTIONS=$SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT GWC_PC_ID=$pcId k6 run $K6_RUN_OPTIONS ''$SCENARIO/index.js'' &
pids[${i}]=$!
((i=i 1))
done
fi
# wait for all pids
for pid in ${pids[*]}; do
echo "Waiting for" $pid
wait $pid
done
Also added #!/bin/bash
to the top of this script file.