0

I am trying to build a mailer. Everything works just fine, but it's too slow.

Here is the code for the bash file

#!/bin/bash

FILE=$(cat $1)

for i in $FILE
do
php send.php $i
done

My question is how can I make it work faster because it runs very slowly. I have already tried changing the sleep but no success.

How could i do it run like: select 10 lines and do php send.php $i1 php send.php $i2 etc

Greg Owen
  • 25
  • 6
  • `sleep 1` sleeps for a second. If you want to sleep less time, check out this answer: https://serverfault.com/a/469252/353480 – Sainan Sep 21 '17 at 17:07
  • already tried that but with no success. i need to make it run simultaneous executions. about 10 at the same time. any suggestion for that ? – Greg Owen Sep 21 '17 at 17:16
  • @GregOwen How long is it taking per iteration? Removing the `sleep` command *should* speed it up by 1 second per iteration; if not, something weird is going on that isn't apparent from what you've written, and unless we understand what that is, we can't really help. Also, are you sure it's safe to run more than one instance of `send.php` at once? For instance, if `send.php` creates a temp file with a consistent name, all instances will write over/read from/etc each others temp files. Finally, what's the format of the file being read? – Gordon Davisson Sep 21 '17 at 17:28
  • it takes about 1 second for each send. i want to try and see what is happening if it runs multiple times but i don't know how.for example it could select 10 lines and do php $i1 send.php; php $i2 send.php and so on. i am a beginner. – Greg Owen Sep 21 '17 at 17:31
  • i have removed the sleep. – Greg Owen Sep 21 '17 at 17:38
  • https://stackoverflow.com/questions/19543139/bash-script-processing-commands-in-parallel – George Vasiliou Sep 21 '17 at 17:43
  • Thanks george. I have tried adding php send.php $i & multiple times but it will send multiple emails. – Greg Owen Sep 21 '17 at 18:00

3 Answers3

1

Run the php command in the background by ending the command with &. The correct way to iterate over the lines of a file is which a while loop running read, not a for loop.

while IFS= read -r i; do
  php send.php "$i" &
done < "$1"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • './start3: line 6: syntax error near unexpected token while ./start3: line 6: `while IFS= read -r i; do' – Greg Owen Sep 21 '17 at 17:44
  • I have solved the problem. Now the problem is that it goes too fast. how can i pause it each 10 sends ? – Greg Owen Sep 21 '17 at 18:03
  • You do what https://stackoverflow.com/a/46349598/1126841 suggested; if your version of `sleep` doesn't support fractional values (as your comment suggests), you'll have to modify `send.php` to sleep a little itself before exiting. – chepner Sep 21 '17 at 18:10
  • i have tried that but it's still the same. i have added `sleep` to `send.php` but it's still too fast. it needs something in the bash file to slow it down a little bit – Greg Owen Sep 21 '17 at 18:18
0

each iteration take at least try to change sleep 1 by sleep 0.5 or less.

0

Since you want to run multiple instances of the send.php at the same time, I would recommend you install screen and split your file into 10 parts and wrap your existing script into a script like this:

#!/bin/bash

for i in {1..10}
do
    screen -dms "send-$1" bash sender.sh "list-part-$i.txt"
done
Sainan
  • 1,274
  • 2
  • 19
  • 32