This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call another PHP script and return control to user before the other script completes
How to run a PHP script asynchronously from a another PHP script?
I have some problems on using exec() in PHP but I have no idea to solve it.
I want to use exec() to execute another php script in same directory 'asynchronously'.
I open a.php in browser to call b.php but it doesn't work. (the localhost is xampp on windows 7)
a.php:
<?php
exec('php \b.php', $output, $r);
print_r($output);
print_r($r);
?>
b.php:
#!/usr/bin/php
<?php
echo time();
?>
The output in browser
Array ( ) 1
I am super beginner on using something related PHP CLI... I have no idea about this...
Can anyone give some simple examples? For example, what should be written in a.php and b.php.
Thanks a lot if you can give me some guides or advice!
EDIT:
I tried following code and it works but it does not run 'asynchronously'... How can I redirect the output?
Open callexec.php in browser.
callexec.php
<?php
exec('C:\xampp\php\php.exe testexec.php');
?>
testexec.php
<?php
echo "start: ",time(),"\n";
sleep(10);
echo "\n";
echo "end: ",time(),"\n";
?>
Thanks again.
As you are already telling your shell-script that it is a php script with #!/usr/bin/php, you don't need to invoke php for your script, you can just call it like any script from the command line:
exec('b.php', $output, $r);
By the way, I am assuming that you can call b.php from the command line without any problems.
Why make things complicated? Just use include or require?
There might be a problem with port numbers if you have another server on the same platform. For instance it could be an oracle database server, mysql, Apache tomcat, etc. Please confirm whether the port numbers of the different servers are the same.
Related
This question already has answers here:
How can I debug exec() problems?
(4 answers)
Closed 3 years ago.
attempting to run a shell script using PHP. the shell script contains:
'cd /data/nfsshare; python /data/nfsshare/kron_hull.py> /data/nfsshare/log.txt'
that is running a python script named kron_hull.py
the php code im using is:
if ($_POST['visual']) {
shell_exec("sh test.sh");
echo "working! woot woot";
} else {
echo "not working";
}
i am using the echo's as a way to tell if its running the if statement correctly, and i am able to successfully echo "working! woot woot", but the shell file does not run, because the script is meant to create a new file on the server, which it does not.
the shell file works when you put it directly in the linux terminal as "sh test.sh" so the issue is not with the shell file.
i also ran shell_exec('whoami'); and shell_exec('ls -lart'); and was able to echo both of those, so i don't believe it is a problem with the shell_exec() command.
my problem is: i dont believe the shell script is even running when put into the shell_exec() command, and I dont know a better way to check besides looking for the new file its supposed to create, which i get none.
any help is appreciated, if you need me to do anything for testing, or need more information, lmk and ill be glad to answer.
i checked the related questions and they did not help my case
One way to check whether the shell command executed correctly is to use the exec() function instead. exec() gives you the ability to see the return value and the output in a variable.
You can build a error checking execution like this:
if ($_POST['visual']) {
exec("sh test.sh", $output, $return);
// Return will return non-zero upon an error
if (!$return) {
echo "working! woot woot";
} else {
echo "Error with shell script";
}
}
To see error codes you can look here.
I have a python script, which I am calling from the php file.
My output is coming as expected, when run as python test.py
But when I am trying to run the php file through the browser, I am not getting the output.
However, on commenting the following lines, I am able to get some results. But I want these lines to run.
with open(filen,'w') as f:
f.write(str("write date"))
I am not sure, what's happening. Can anyone explain what difference its making when php reaches these lines.
Thanks
the question is not clear. I'm guessing you want to execute a python script from php. Your python script does not output anything but write to a file.
executing a python file from php
<?php
print shell_exec("python test.py");
?>
this question may help you if the code above doesn't work: php shell_exec() command is not working
please edit your question for a more accurate response.
I have a free webspace hosted by altervista where i have one script python and one script php. What i want to do is call the python script using the php script. I googled for a solution and i found several questions about it, i tried different proposed solutions but no one worked for me.
Here the php script code:
<?php
$command = escapeshellcmd('test.py');
$output = shell_exec($command);
echo $output;
?>
and here the python script code:
print("Hello")
When i run the php script nothing happens.
What's wrong?
Thanks
Your hosting company might have disabled the shell-exec function for security reasons. I'm not saying it is the case, but you should check that first before even trying to run shell_exec.
Seealso:
PHP - How to know if server allows shell_exec
Plus, back in 2012, it wasn't possible with Altervista...
Well, I have to call a python script from a php one but it does nothing. I've been searching but i couldn't fix it. The script creates 2 files and writes things in the terminal.
Here's my code:
exec('/usr/local/bin/python /usr/local/script/myScript.py '.$path.' '.$specie.' '.$pvalue.');
Thank you very much.
I am running through a weird problem with exec's in PHP. When I load in the browser the script x.php, the browser hangs and when I run a ps I can see multiple threads being created. The only way to stop them is by restarting apache.
However, instead of running a php script, if I do something like system('ls'), it works fine. So it seems to be problem when a PHP script tries to run another script using exec/system/passthru (I've tried them all).
x.php is defined as following:
<?php
var_dump(system('php -f t.php'));
?>
t.php is defined as following:
<?php
echo 'Hello world';
?>
Take a look at first comment in exec() manual.