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);
?>
PHP

Avatar of undefined
Last Comment
myyis

8/22/2022 - Mon
hielo

You need "include":
<?php
// test.php already calls session_start()
include ('test.php');
?>
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.
hielo

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ray Paseur

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

>> 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";
?>
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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 ( )
myyis

ASKER
ASKER CERTIFIED SOLUTION
hielo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
hielo

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

myyis

ASKER
When I make this change the script keeps on working and nothing happens.
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hielo

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

ASKER
After dozens of similar errors I had to reset the server