I am using XAMPP on Windows 7 to run a localhost, and I'm trying to use the PHP exec function to execute a Node.js script.
I am able to successfully use the exec function to run PhantomJS scripts, but when I try to do the same thing for Node.js, it doesn't work.
He's an example of a PHP script that properly runs a PhantomJS script:
<?php
exec('/phantomjs/bin/phantomjs /phantomjs/scripts/test.js', $output);
print_r($output);
And here's a similar example of a Node.js script that outputs an empty array every time:
<?php
exec('/Program Files (x86)/nodejs/node /Program Files (x86)/nodejs/test.js', $output);
print_r($output);
I'm sure that all of my paths are correct and that the Node.js script executes correctly whenever I execute it directly from the command line, but I can't get anything to return from the Node.js script when I run it from the PHP exec command. I also tried the following script, but I still get nothing:
<?php
exec('/Program Files (x86)/nodejs/node -v', $output);
print_r($output);
Any advice on what I'm missing would be greatly appreciated. Thank you.
Related
I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. But I am not getting any output. When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. I'm running Python script from PHP using the following command:
$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');
echo $result
However, if I run a simple Python program from PHP it works.
PHP has the permissions to access and execute the file.
Is there something which I am missing here?
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);
var_dump($output);
2nd Parameter of exec will give output
EDIT:
exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);
2>&1 - redirecting stderr to stdout. Now in case of any error too, $output will be populated.
First Check your python PATH using "which python" command and check result is /usr/bin/python.
Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash.
Than run these commands
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result);
echo $result
I am unable to execute a source command in linux using php.All other commands are working except this one. I need to execute the following command.
source /root/Envs/ate/bin/activate
This activates the ate-Automatic Test Equipment.Once I activate it then I need to run a python script as the script accesses the remote server.
I am able to manually run it but I am creating a tool which will automatically do it.
<?php
exec("source /root/Envs/ate/bin/activate", $output, $return);
echo "Command returned $return, and output:\n";
echo exec("python box_upgrade-pradeepa.py");
?>
The above commands returns 1 which means there is an error.But I am not sure how to run the 'source command'. The python script will run only if the source command is successful.(the python command is correct as I replaced hello.py and it ran fine.)
Could you pls help me as I am really stuck for a week?
Thanks a lot..
I found out the error. Since I am doing it using php (for a web tool) the user is Apache. 'Apache' user is unable to access the script in root folder. Moving it to another directory, I am able to run the script fine.
Thanks all..
I am trying for last 3 hours to tell PHP to run a simple file. I am using wamp server for windows in local host (Windows 8)
I've tried with exec() working with:
echo exec('whoami');
I got response nt authority.
Also tested with:
if(function_exists('exec')) {
echo "exec is enabled";
}
So it probably works?
I am trying to run a file called tester.php
When I include it, its working, when I require it its working. I need to execute it in background. When I refresh file, code is working without any error, it writes to the database normally.
When i try to exec it its not working.
I tried :
exec("php http://localhost/diplomski/program/defender/tester.php");
exec("php-cli http://localhost/diplomski/program/defender/tester.php");
exec("http://localhost/diplomski/program/defender/tester.php");
Not working, also tried:
exec("php http://127.0.0.1/diplomski/program/defender/tester.php");
exec("php-cli http://127.0.0.1/diplomski/program/defender/tester.php");
exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php")
Not working also tried:
exec("php tester.php");
exec("php-cli tester.php");
exec("tester.php");
Also tried:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("D:\wamp\bin\php\php5.3.13\php-win.exe -f d:\wamp \www\diplomski\program\defender/tester.php", 0, false);
Tried this, its refreshing infinitely and not working:
exec("php d:\wamp\www\diplomski\program\defender/tester.php");
exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php");
exec("d:\wamp\www\diplomski\program\defender/tester.php");
I'm starting to pull my hair out here. First time I'm trying to use exec() and I'm not very good with it or with the commands.
Give the full path to the PHP executable and the full path to the PHP script. You can save the output in $output to see what the script produced:
exec("d:/path/to/php.exe d:/wamp/www/diplomski/program/defender/tester.php", $output);
print_r($output);
1) What version of php? If it is older then 5.4.0 php can be in safe mode, when safe mode is enabled, you can only execute files within the safe_mode_exec_dir.
2)Note to this function in php.net
Note:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
3) So you can try this How to make php script run another php script you can try this
<?php
$somearg = escapeshellarg('blah');
exec("php file2.php $somearg > /dev/null &");
4) You can create a scheduled task How to run a PHP file in a scheduled task (Windows Task Scheduler)
In addition to the earlier answers, let me add that if you want to execute a PHP file from within PHP, you may want to consider the PHP function include instead:
include $path."tester.php"
I guess that would be (much?) more efficient than to spawn a new shell which executes a new instance of PHP which executes the file. But of course the choice of the "better option" may depend on the context.
I want to execute a command in ubuntu terminal. When I directly run the command in terminal, it runs without any problem. But What I actually want to do is to execute this command via PHP.
chdir('/home/thilini/FYP/testone/bin/');
exec('./mindtct input_folder/filename output_folder/filename');
The php code I wrote is shown above. I am using ubuntu 10.10 and the LAMP configuration. chdir is working fine and I have successfully moved from /var/www/ to /home/thilini/FYP/testone/bin/ (where I have the executable mindtct). But exec is not working. (mindtct is an executable which convert the file in the input folder to another format and store it in the output_folder under the given name).
What am I doing wrong?
The problem was an issue in the path. A forward slash was missing.
If you're running below php 5.4,check "safe_mode" in your ini file.
http://www.php.net/manual/en/features.safe-mode.functions.php
You probably want
exec('./mindtct input_folder/filename output_folder/filename');
Maybe you should set error_reporting(-1) in your script so you get some errors
You want to use shell_exec(), not exec().
shell_exec() executes a command in the terminal, whereas exec() opens an application.
$results = shell_exec('./mindtct input_folder/filename output_folder/filename');
print_r($results);
This will execute the command, store it in results, and then print_r the results in array format.
http://php.net/manual/en/function.exec.php
http://php.net/manual/en/function.shell-exec.php
I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!