0

I am calling Bash script from PHP, but it looks like PHP script won't complete until bash script runs complete.

exec(dirname(__FILE__) . '/sendemail.sh');

In bash script, I am sending 100s emails and I have sleep in each time. If I start the script from command line everything fine. But when I execute PHP from web server, php is never get completed and its waiting forever to load the page.

### Get emails one at a time from PHP generated list and start loop. 
cat /home2/xx/www/sendmail/phpmailer/libs/people.txt | while read line 
do
sleep 30
echo "$line" 

### Set path of program that will run.
xmail="/usr/sbin/sendmail"

### Get subject generated from sendpage.php "not sure on name?
xsub=$(cat /home2/xx/www/sm/subject.txt)

### Show from in email.
xfrom="xx@yy.net"

### Get body generated from sendpage.php "not sure on name?
xmsg=$(cat /home2/xx/www/sm/body.txt)

### Compose emails one at a time.
"$xmail" "$line" << EOF
subject:$xsub
from:$xfrom
$xmsg
EOF

### End loop when while is complete.
done
mtkilic
  • 1,213
  • 1
  • 12
  • 28
  • From the [documentation](https://secure.php.net/manual/en/function.exec.php): *"If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends."* 30s * 100 are 50 minutes. I.e. PHP has to wait for 50 minutes until it can send a response to the client. – Felix Kling Nov 12 '17 at 03:02
  • @FelixKling I dont want PHP to run in background, from php I create txt files, at the end of php script, I call bash script to read this txt files. So, bash will run in the background but php need to complete executing right after calling bash script. – mtkilic Nov 12 '17 at 03:08
  • Then you have to wait for 50 minutes. `¯\_(ツ)_/¯` – Felix Kling Nov 12 '17 at 03:15
  • @FelixKling I dont think I explain myself well enough :/ there has to be away. Thanks for feedback though – mtkilic Nov 12 '17 at 03:19
  • Maybe I misunderstood. What to do to make PHP *not* wait for the script to finish is explained in the part I quoted from the docs. – Felix Kling Nov 12 '17 at 03:22
  • @Erik if I understand that link I have to correct my syntax to `exec(dirname(__FILE__) . '/sendmail.sh > /dev/null &');` ? – mtkilic Nov 12 '17 at 03:32
  • 1
    yup that looks right – Erik Nov 12 '17 at 03:33
  • 1
    @Erik that worked! thanks – mtkilic Nov 12 '17 at 03:54

0 Answers0