php exec()  hangs

myyis
myyis used Ask the Experts™
on
When I run this, it just keeps on working  and nothing happens. What can I do? Thank you.
<?php
session_start();
exec('php test.php');
?>

this is  test.php

<?php
session_start();
$uid=$_SESSION['userIdsession'];
mail('xxx@gmail.com','dd'.$uid);
?>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2008
Top Expert 2008

Commented:
You need "include":
<?php
// test.php already calls session_start()
include ('test.php');
?>

Author

Commented:
I need to run a parallel script  that's why I tried exec.

include or even an ajax request does not run parallel as far as I know.
Expert of the Year 2008
Top Expert 2008

Commented:
Try:
<?php
session_start(); 
$uid=$_SESSION['userIdsession'];

//pass the uid as a command-line parameter
$command = '/usr/bin/php -f /path/to/test.php '. $uid;
exec( "$command > /dev/null &", $arrOutput );
?>

test.php
<?php
$uid=$argv[1];

$to = 'xxx@gmail.com';
$subject ='dd'.$uid; 
$message='Some Message';

//be sure to include a "From:" headers.  Some mailers will not send out emails if you don't have a From header.
$headers="From: you@yourcompany.com\n";

mail($to,$subject,$message,$headers);
?>

Open in new window


>> I need to run a parallel script
You may also want to take a look at fork: http://php.net/manual/en/function.pcntl-fork.php
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Most Valuable Expert 2011
Top Expert 2016

Commented:
IIRC, You can also run a parallel script by starting it via a cURL POST request.  You can set the timeout to 1 second.

Author

Commented:
Hi Hielo,
I have tried your method, but nothing happens. I have checked and see that exec is not disabled.
May the problem be the path of the command line or the path of the page to be run?
I also cannot get an error message. Thank you.
Expert of the Year 2008
Top Expert 2008

Commented:
>> but nothing happens
You mean you don't get any errors correct?
Are you on a Linux system? Are you able to open a terminal? If so, first verify that the path to php is correct.  At a command prompt, type :
 pwd

If it shows something different from /usr/bin/php, then make the necessary adjustments to the $command.  Also, try executing the command from the terminal (but without the "> /dev/null &"):
/usr/bin/php -f /path/to/test.php

Lastly, try adding the path to the tes.php
<?php
#!/usr/bin/php
echo "Executing test.php";

$uid=$argv[1];

$to = 'xxx@gmail.com';
$subject ='dd'.$uid;
$message='Some Message';

//be sure to include a "From:" headers.  Some mailers will not send out emails if you don't have a From header.
$headers="From: you@yourcompany.com\n";

mail($to,$subject,$message,$headers);
echo "Done";
?>

Author

Commented:
I can't  have the terminal but the image of the root directory is attached if it is useful

To be simpler I changed test.php  as
<?php
#!/usr/bin/php
echo "Executing test.php";
?>

test2.php
<?php
echo getcwd() . "\n";
$command = '/usr/bin/php -f /path/to/test.php ';
exec( "$command > /dev/null &", $arrOutput );
print_r($arrOutput);
?>

test2 returns
 /home/mywebsite/public_html Array ( )

Author

Commented:
Expert of the Year 2008
Top Expert 2008
Commented:
OK, then inf test is currently saved in "/home/mywebsite/public_html", then you need:

$command = '/usr/bin/php -f /home/mywebsite/public_html/test.php ';

Open in new window


also, verify the path to php by executing:
test2.php
<?php 
echo getcwd() . "\n";
echo `which php`; /* if the output of this line is not /usr/bin/php, then adjust the command below with this path */
$command = '/usr/bin/php -f /path/to/test.php ';
exec( "$command > /dev/null &", $arrOutput );
print_r($arrOutput);
?>

Open in new window

Author

Commented:
test2.php is as below

<?php
echo `which php`;
$command = '/usr/bin/php -f /home/mywebsite/public_html/test.php ';
exec( "$command > /dev/null &", $arrOutput );
print_r ($arrOutput);
?>

the output is  
/usr/bin/php Array ( )

But I should see  "Executing test.php", No?
Expert of the Year 2008
Top Expert 2008

Commented:
>> But I should see  "Executing test.php", No?
No.  The output is being redirected to /dev/null.  If you redirect anything to /dev/null, the output simply "disappears".  If you want to see that output, get rid of the redirection:

<?php
echo `which php`;
$command = '/usr/bin/php -f /home/mywebsite/public_html/test.php ';
/* exec( "$command > /dev/null &", $arrOutput ); */
exec( "$command ", $arrOutput );
print_r ($arrOutput);
?>

Open in new window

Author

Commented:
When I make this change the script keeps on working and nothing happens.

Author

Commented:
I get this error from whm

Excessive processes running
Time:          Tue Jul 28 00:00:38 2015 +0300
Process Count: 2516 (Not killed)

Process Information:

User:mywebsite PID:15654 PPID:6634 Run Time:136(secs) Memory:188028(kb) exe:/usr/bin/php cmd:/usr/bin/php /home/mywebsite/public_html/test2.php
Expert of the Year 2008
Top Expert 2008

Commented:
Hmmm, I wonder if the server is receiving legitimate traffic or is there a rogue bot?  If I were you, I would contact the server administrator and have him/her investigate further.

Author

Commented:
After dozens of similar errors I had to reset the server

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial