If you want to run them in parallel you can use GNU parallel:
#!/usr/bin/env bash
file=$1
status_func () {
local _url="${1:-/dev/stdin}"
local _urlstatus
_urlstatus=$(curl -H 'Cache-Control: no-cache' -o /dev/null --silent --head --insecure --write-out '%{http_code} ; %{redirect_url}' "$_url")
printf 'Status: %s\n' "$_urlstatus"
}
export -f status_func
cat "$file" | parallel -j20 status_func
The -j option will set the maximum number of simultaneous jobs, the default is the number of threads on your system but you may need to tweak that number for optimal results.
Note you will have to add your commands into a function (or alternatively a separate script altogether) and export that function so that parallel knows about it.
If you were to put your commands into a standalone script than you could just run it from the command line like:
cat "$file" | parallel -j20 ./my_script.sh
Note this script should expect only a single URL sent via stdin.
Alternatively if you cannot use GNU parallel but still want to run multiple jobs at once you could just execute them as background jobs but will most likely want to limit the number of simultaneous processes. That can be accomplished with the following:
#!/usr/bin/env bash
file=$1
max_bg_procs () {
local max_number=$((0 + ${1:-0}))
while :; do
local current_number=$(jobs -pr | wc -l)
if [[ $current_number -lt $max_number ]]; then
break
fi
sleep 2
done
}
status_func () {
local _url="${1:-/dev/stdin}"
local _urlstatus
_urlstatus=$(curl -H 'Cache-Control: no-cache' -o /dev/null --silent --head --insecure --write-out '%{http_code} ; %{redirect_url}' "$_url")
printf 'Status: %s\n' "$_urlstatus"
}
while IFS= read -rd url; do
max_bg_procs 20
status_func "$url" &
done < "$file"
curl
commands in paralel?-H 'Cache-Control: no-cache'
is used to download only the header and avoid downloading the whole page ! All I need is HTTP status code and - in case of redirection - the new URL--head
or-I
will cause it to download only the header.