Hi I am unable to run the .sh file using my php code.
Files: index.php and .sh files are in the same directory.
What I have tried:
echo shell_exec('sh shell_file.sh'); //Did not execute
echo shell_exec('shell_file.sh'); //Did not execute
echo exec('shell_file.sh'); //Did not execute
But when I run the shell_file.sh file manually it does execute.
Try like this:
What you need to do is call the file with a program. Call it with bash or sh as suggested in the comment:
echo shell_exec('sh /shell_file.sh');
Another option could be:
$contents = file_get_contents('/shell_file.sh');
echo shell_exec($contents);
I think the first option would be better however.
Related
I want to run bat file via php.This bat file contains power shell script.I am able to run this via cmd but not via php.
AwsExecute.bat is,
powershell -command ./AWSExecuter.ps1
This AwsExecuter.ps1 which is inside the C:\AWS\Distributed-setup-3 folder.
In php I have tried like this,
Trail.php which is in http://localhost/performance folder.
<?php
exec('C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
As well as,
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
Both are not working here, I tried to another bat file which just creates a php file it works fine.
That is First.bat,
echo off
break>"C:\Users\Vidya\Desktop\performance.php"
echo done
Try running system
system("cmd /c C:\AWS\Distributed-setup-3\AwsExecute.bat");
For more on system read this
I have a problem when you run a script from php the problem is that it doesn't do what must be done, this is the php code
? php
echo "starting execution";
echo exec("./sc");
?>
and in the code of the script this is sc
mv text.txt /var/www/files/
does not work does not move the file archive there is I've added the chmod permismos but doesn't appreciate any suggestion
First, I would suggest to execute the script file manually from command line(console) and check if the script file is working properly. If that doesn't work, then it won't work in php too.
Try:
echo "starting execution";
echo exec("./sc", $output);
var_dump($output);
contents of sc :
mv text.txt /var/www/files/ 2>&1
If the file is not moved, it must throw an error which you should be able to see in $output.
My guess would be that the error must be related to permission.
I am trying to use a PHP script to call a shell script and print the output to the browser.
I have confirmed that all permissions settings are set correctly, both the PHP and the bash script have full control.
If I run the shell script in the current directory (same directory as the PHP file) then it works fine. But if I try to run the shell script from /some/other/directory/shell.script, then it does not work. Why is this so?
I have tried to chdir() to the other directory, but after running a getcwd(), it never changed directories.
I have also tried the exec command by itself and it does not work.
<?php error_reporting(E_ALL); ini_set('display_errors',1);
echo "<h1>EDS Count Report</h1><p>";
$output=shell_exec('cd /app/script/catalog/ && ./EDScount_report.sh');
echo $output;
?>
contents of SHELL script:
#!/bin/bash
echo "this is a test!"
Try this please
<?php
$output=shell_exec('sh /path/to/otherdirectory/shellscript.sh');
echo $output;
?>
if you are certain that you can access that dicertory , you can use also this:
<?php
$output=shell_exec('cd /path/to/otherdirectory/ && ./shellscript.sh');
echo $output;
?>
i am new to exec function and need help with executing an external php file. My code of the files are as follows
Script.php(main file) :
<?php
$path = 'C:/xampp/htdocs/user/execute.php';
exec($path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>
execute.php(calling file) :
for($i=0;$i<10;$i++){
echo "hello"."<br>";
}
trying to execute the calling file
exec is for executing system functions, not for running scripts. (Take a look at the manual, it's helping: http://php.net/manual/de/function.exec.php)
To achieve what you want, you could pass the path to php executable and add the script as parameter, like this:
<?php
$phpExecutable = 'C:/xampp/bin/php.exe'
$path = 'C:/xampp/htdocs/user/execute.php';
exec($phpExecutable." ".$path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>
Should work. I do not know where your php executable is located, so please adapt it to your location.
Happy coding.
First of all, as guys said in comments, you don't need exec() here at all. You can just include that file.
Anyway, exec() function executes external program. A .php file is not a program, it's just a script that is executed by a program called php.
So you can run it like:
exec('php ' . $path, $output,$return);
php also can require you to give full path to its executable if it's not available globally.
http://php.net/manual/en/function.exec.php
Adding
exec("php execute.php > /dev/null &");
solved my problem..Thanks all
I am using WAMP server on my system to execute php scripts.
I want to execute a script test.php from my main script main.php.
For that I am using exec function like this exec('php test.php');. In test.php I have given one echo statement.
But when I run my main.php script from the browser I am not able to see output of test.php script.
What am I doing wrong ? please suggest.
You have to give the proper path of php.exe
exec("c:\wamp\<where ever you exe is>/php.exe test.php");
so it has to be a proper path
use this command
echo exec('php test.php', $output); //this will print your echo statement.
print_r($output);