我目前有一个shell脚本,它依赖于像这样的curl命令:
curl --request POST -u name:pass -H "Content-Type: application/json"
--data "{data}" https://url.com --cacert ./my_crt
Run Code Online (Sandbox Code Playgroud)
我不需要命令的响应,并且此命令处于大的for循环中,因此等待响应需要花费大量时间.
那么,在bash中有没有办法做同样的事情,但是没有等待响应?
小智 11
你可以只将其与背景&
,并防止你可以重定向输出stdout
和stderr
向/dev/null
.
curl --request POST -u name:pass -H "Content-Type: application/json" \
--data "{data}" https://url.com --cacert ./my_crt > /dev/null 2>&1 &
Run Code Online (Sandbox Code Playgroud)
tha*_*guy 11
如果您想要快速发出大量请求,并且不关心输出,那么您应该做两件事:
对于小型请求,通常在1个连接上每个请求10个请求要快得多,而在10个连接上每个请求1个请求.对于Henry的HTTP post测试服务器,差异是2.5x:
$ time for i in {1..10}; do
curl -F foo=bar https://posttestserver.com/post.php ;
done
Successfully dumped 1 post variables.
View it at http://www.posttestserver.com/data/2016/06/09/11.44.48536583865
Post body was 0 chars long.
(...)
real 0m2.429s
Run Code Online (Sandbox Code Playgroud)
VS
$ time {
array=();
for i in {1..10}; do
array+=(--next -F foo=bar https://posttestserver.com/post.php ) ;
done;
curl "${array[@]}";
}
Successfully dumped 1 post variables.
View it at http://www.posttestserver.com/data/2016/06/09/11.45.461371907842
(...)
real 0m1.079s
Run Code Online (Sandbox Code Playgroud)
这里sem
从GNU并行将并行连接的数量限制为4.这是一个更好的后台和等待版本,因为它将始终确保满容量.
for i in {1..20}
do
sem -j 4 curl -F foo=bar https://posttestserver.com/post.php
done
sem --wait
Run Code Online (Sandbox Code Playgroud)
您想要的并行请求数取决于主机的强大程度.一个现实的数字可能是32+
结合这两种策略,你应该看到没有DoS自己的大幅加速.