![방법: 동시 cURL 다운로드?](https://rvso.com/image/1539560/%EB%B0%A9%EB%B2%95%3A%20%EB%8F%99%EC%8B%9C%20cURL%20%EB%8B%A4%EC%9A%B4%EB%A1%9C%EB%93%9C%3F.png)
컬을 사용하여 여러 파일을 동시에 다운로드하려면 어떻게 해야 합니까? 여러 컬 프로세스를 생성해야 하는 경우에도 명령줄이나 셸 함수에서 어떻게 수행할 수 있나요? 예를 들어:
$ multiCurl <download 1> <download 2> <download 3>
multiCurl () {
curl <download 1>
curl <download 2>
curl <download 3>
}
또는
for link in $(cat download.list); do
curl <link 1>
curl <link 2>
curl <link 3>
curl <link 4>
done
답변1
어쩌면 명령을 사용하여 parallel
여러 파일을 동시에 다운로드할 수도 있습니다.
여기서는 원본 파일 이름을 예약했다고 가정합니다.
#!/usr/bin/env bash
funcDownloadOperation(){
link="${1:-}"
curl -fsSL -O "${link}"
if [[ -s "${link##*/}" ]]; then
echo "file ${link##*/} download successfully!"
else
echo "fail to download link ${link}"
fi
}
export -f funcDownloadOperation
cat download.list | parallel -k -j 0 funcDownloadOperation
parallel
Linux 시스템에 유틸리티를 수동으로 설치해야 합니다 .
답변2
&
백그라운드에서 실행되도록 하려면 각 명령의 끝에 추가하기만 하면 됩니다 .
curl <link 1>&
결국에는 이러한 명령이 끝나는 시기와 반환 코드가 무엇인지 알고 싶은지 여부에 따라 달라집니다. 그렇다면 다음과 같이 시도해 보세요.
set -a pids # Array of the process ids of background commands
for url in $whatever_the_list_of_urls
do
curl $url& # start CURL in background
pids[${#pids[*]}]=$! # remember PID
done
# all started, now wait for them
for pid in ${pids[*]}
do
wait $pid
print "Sensed end of $pid, rc:=$?"
done