I have to send some cURL request in a parallel way. At one side I have the curl.sh script (for security reasons data has been hidden):
#!/bin/bash
SERVER=...
PORT=...
CONTEXTPATH=...
SERVICE=...
CERTIFICATETYPE=...
CERTIFICATE=...
KEYCERT=...
HEADER=...
CONTENT="Content-Type:text/xml;charset=UTF-8"
if [ "$1" != "" ] && [ "$2" != "" ]
then
echo "Executing curl script for $1 > $2"
curl -s -d @$1 --cert $CERTIFICATE:$KEYCERT --cert-type $CERTIFICATETYPE http://$SERVER:$PORT/$CONTEXTPATH/$SERVICE -H $HEADER -H $CONTENT > $2
else
echo "ERROR not valid arguments: curl.sh[ARG1=$1,ARG2=$2]"
fi
So this script, will be call as:
$ curl.sh requestData outputFile
Then, in another parallel.sh script I have:
#!/bin/bash
MODE=$1
REQUEST=
if [ "$MODE" != "" ]
then
if [ $MODE -eq 1 ]
then
REQUEST=requestFileOne.xml
fi
if [ $MODE -eq 2 ]
then
REQUEST=requestFileTwo.xml
fi
else
echo "ERROR no valid parameter"
echo "./parallel.sh 1|3"
echo " mode 1: send request 1x1"
echo " mode 3: send request 3x3"
fi
if [ "$REQUEST" != "" ]
then
echo "START CURL PARALLEL for $REQUEST"
time ./curl.sh $REQUEST ./out/out_1.xml & ./curl.sh $REQUEST ./out/out_2.xml &
./curl.sh $REQUEST ./out/out_3.xml & ./curl.sh $REQUEST ./out/out_4.xml &
./curl.sh $REQUEST ./out/out_5.xml & ./curl.sh $REQUEST ./out/out_6.xml
wait
fi
echo "END CURL PARALLEL"
As you can see, now I do
$ ./parallel.sh 1
But if I want to change the number of parallel calls, I have to edit the parallel.sh script file, and I would want something like:
$ ./parallel.sh file number
Where {file: 1|2} is the data file to use with cURL, and {number:X} will be the number of parallel calls.
Firstly, I thought of building the parallel executing command inside a loop
for i in 1..$2
do
stringVar="$stringVar | ./curl.sh $REQUEST ./out/output_$i.xml"
done
for i in 1..$2
do
result=`./curl.sh $REQUEST ./out/output_$i.xml`
done
for i in 1..$2
do
./curl.sh $REQUEST ./out/output_$i.xml
done
But, obviously none of this loops works as expected. At first loop, I was trying to save the curls command as String, so later I will run the String as command.
Any idea? Thanks in advance.