¿Cuál es la mejor manera de ejecutar 5 curlsolicitudes paralleldesde un script bash? No puedo ejecutarlos en serie por razones de rendimiento.
¿Cuál es la mejor manera de ejecutar 5 curlsolicitudes paralleldesde un script bash? No puedo ejecutarlos en serie por razones de rendimiento.
Respuestas:
Use '&' después de un comando para poner en segundo plano un proceso y 'esperar' para esperar a que finalicen. Use '()' alrededor de los comandos si necesita crear un sub-shell.
#!/bin/bash
curl -s -o foo http://example.com/file1 && echo "done1" &
curl -s -o bar http://example.com/file2 && echo "done2" &
curl -s -o baz http://example.com/file3 && echo "done3" &
wait
xargs tiene un parámetro "-P" para ejecutar procesos en paralelo. Por ejemplo:
wget -nv http://en.wikipedia.org/wiki/Linux -O- | egrep -o "http://[^[:space:]]*.jpg" | xargs -P 10 -r -n 1 wget -nv
Referencia: http://www.commandlinefu.com/commands/view/3269/parallel-file-downloading-with-wget
Yo uso GNU paralelo para tareas como esta.
curla gnu parallel?
Aquí hay un curlejemplo con xargs:
$ cat URLS.txt | xargs -P 10 -n 1 curl
El ejemplo anterior debe curlcada una de las URL en paralelo, 10 a la vez. El -n 1está allí para que xargssolo use 1 línea del URLS.txtarchivo por curlejecución.
Lo que hace cada uno de los parámetros de xargs:
$ man xargs
-P maxprocs
Parallel mode: run at most maxprocs invocations of utility at once.
-n number
Set the maximum number of arguments taken from standard input for
each invocation of utility. An invocation of utility will use less
than number standard input arguments if the number of bytes
accumulated (see the -s option) exceeds the specified size or there
are fewer than number arguments remaining for the last invocation of
utility. The current default value for number is 5000.