Why does my bash script hang?

I’m working on a bash script that will check +1000 domains if they are expired. I use a a for loop to iterate over all users in /var/cpanel/users/*. It works great for like the 10 first users (loops) then it just hangs.

A weird thing is that I can stop the script with Ctrl+Z and then start the script again with fg and it continues to work normal for about +10 users but then it hangs again.

This is my scirpt:

# File that will have the result.
file="domain-result.txt"

printf "USERttDOMAINtttREPORTn" > "$file"
printf "n" >> "$file"

# For loop to iterate over all users in cpanel.
for z in /var/cpanel/users/*;
do
     # Only files can be used.
     if [[ -f "$z" ]]
     then

     # Get the domain name.
     awk -F'=' '/DNS=/ {print $2}' "$z" | while read row;
     do
            # If there's no domain name than skip to next account.
            if [[ -z "$row" ]]; then continue; fi

            printf "Checking domain: %s...donen" "$row"

            # Execute whois command on the domain. 
            whois=$( /usr/bin/whois $row | grep 'not found' )

            # Get the username.
            user=$( echo "$z" | awk -F'/' '{print $5}' )

            if [[ -n "$whois" ]]
            then
                    printf "%stt%stt%s - EXPIREDn" "$user" "$row" "$whois" >> "$file"
                    break
             else
                    continue
             fi
      done

      else
            continue
      fi
done

printf "n"
printf "Total: $( sed '1,2d' "$file" | wc -l ) expired domains.n"

This is a sample of how the files in /var/cpanel/users/* look like:

DNS=stackoverflow.com


Source: shell

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.