Home > database >  Execute multiple terminal commands at once with "&" and get an ordered result
Execute multiple terminal commands at once with "&" and get an ordered result

Time:11-23

I need to execute multiple commands (like 2000 at a time) at once and really fast.

One way is to execute them with a "&" between each command which is really fast but I get the results (json format) mixed up so that I can not just load the output as json.

I also tried to use 'parallel'. With that command I get the commands in correct json but it takes way longer.

Is there a way to execute the commands with "&" and not get the output all mixed up?

CodePudding user response:

I doubt spawning 2000 process might be a good idea and parallel would give you more control, having said that, something simple like this could be used

N=2000
for ((n=0; n<N; n  ))
do
    mycommand $n > out-$n.txt &
done
wait
for ((n=0; n<N; n  ))
do
    cat out-$n.txt
done

CodePudding user response:

You can add a message command to JSON with the jq command.

I don't know how you are calling your commands. But you can add additional objects to JSON.

command |jq '.   {"key2": "value2"}'

Example code

command | jq '.   {"commands": \"$(script execute)\"}'
  • Related