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;
?>
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
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.
Hi I am trying to make use of exec command and change the directory to execute a specific command, but change directory is not working
when i execute exec('whoami'); i get proper output
when i execute following code
<?php
ini_set('error_reporting', E_ALL);
$var = exec('cd /root/');
echo exec("pwd");
?>
my directory is not changing to root
I think you need to use chdir() rather than exec('cd /root/'):
<?php
ini_set('error_reporting', E_ALL);
$var = chdir('/root/');
echo exec('pwd'); // Prints /root
This may not work if this is running on a website and the user the HTTP server is running as does not have access to /root.
The reason that exec() doesn't work is that it opens a new subprocess each time. Changing the current directory in one does not affect others spawned after.
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);
I'm trying to run a shell script from a php frontend
heres the shell file (run.sh chmod to 777)
#!/bin/bash
wget -O index.html http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
python hw7-9.py index.html
echo "done";
Heres the php front end
<?php
$output = shell_exec("run.sh");
echo "<pre>$output</pre>";
?>
But it php page doesn't return anything except
<pre></pre>
Have you tried doing error_reporting(-1); at the top of your PHP script. Likely shell_exec is disabled on your server.
Would you try
$output = shell_exec("sh run.sh");
Check what user the php/web server is actually run as - e.g. "www-user" may have no permissions whatsoever to do the things your script is trying to do (and for good reason).