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);
?>
LVL 1
myyisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
You need "include":
<?php
// test.php already calls session_start()
include ('test.php');
?>
0
myyisAuthor 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.
0
hieloCommented:
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
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Ray PaseurCommented:
IIRC, You can also run a parallel script by starting it via a cURL POST request.  You can set the timeout to 1 second.
0
myyisAuthor 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.
0
hieloCommented:
>> 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";
?>
0
myyisAuthor 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 ( )
0
myyisAuthor Commented:
0
hieloCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
myyisAuthor 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?
0
hieloCommented:
>> 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

0
myyisAuthor Commented:
When I make this change the script keeps on working and nothing happens.
0
myyisAuthor 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
0
hieloCommented:
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.
0
myyisAuthor Commented:
After dozens of similar errors I had to reset the server
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.