syf101
syf101

Reputation: 71

Multi threading in command line possible?

I'm using the following command to check the whois information from a list of domains in a text file and then output any lines that contain an email to a new file:

for i in $(cat testdomains.txt); do whois $i| egrep [a-zA-Z0-9]@[a-zA-Z0-9]\.[a-zA-Z0-9]; done >> results.txt

Is there any way to speed this up by checking more than one domain at a time? For example, right now it is going from one domain to the next checking the information. Is there anything I could change in the command to make it check 50 domains at a time?

Upvotes: 0

Views: 4057

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185161

With &, you can run any command in background (so in parallel) :

for i in $(< testdomains.txt); do
    whois "$i" | egrep '[a-zA-Z0-9]@[a-zA-Z0-9]\.[a-zA-Z0-9]' &
done >> results.txt

Note

  • If you put the control operator & at the end of a command, e.g. command args &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Pid of the last backgrounded command is available via the special variable $!
  • every & do a fork(2) in the background
  • see How do I wait for several spawned processes?

Upvotes: 3

Related Questions