Link to home
Start Free TrialLog in
Avatar of myyis
myyis

asked on

php exec() hangs

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);
?>
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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

ASKER

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.
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
IIRC, You can also run a parallel script by starting it via a cURL POST request.  You can set the timeout to 1 second.
Avatar of myyis

ASKER

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.
>> 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";
?>
Avatar of myyis

ASKER

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 ( )
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of myyis

ASKER

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?
>> 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

Avatar of myyis

ASKER

When I make this change the script keeps on working and nothing happens.
Avatar of myyis

ASKER

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
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.
Avatar of myyis

ASKER

After dozens of similar errors I had to reset the server