I am have a PHP script called process_item.php that can be run from terminal. I want to create a web script that can run the same file using exec(). My web script(process_item_online.php) looks like below:
ini_set("display_errors",true);
error_reporting(E_ALL);
$item = $_GET['item'];
echo "Received request for item $item<br>";
$cmd = "/usr/bin/php /var/www/html/process_item.php $item";
exec($cmd);
echo "Processed using command - $cmd";
The script process_item.php has 777 permissions. When I run the above script in browser, I get both these lines printed correctly:
Received request for item 10023444AJK
Executed command - /usr/bin/php /var/www/html/process_item.php 10023444AJK
However nothing else happens. As in, the database is not updated, the logs are not written etc. I do not get any error on the browser either. I am sure this is a permission issue since it works fine from the terminal. Can anyone give me any pointers on how I can make this work from the browser?
Related
I created a php file as shown below.
<?php
$ar = 4600;
$az = "redshift -O ".$ar;
echo "Command executed: ";
echo $az;
system($az,$status); // It is not working on browser but working on terminal.
system("ls",$asd); // It is working on both browser and terminal.
if($status == NULL) {
echo "<h3>Operation not successful</h3>";
}
else
echo "<h3>Temperature Succesfully set to ".$ar."</h3>";
?>
Now, the matter is that
when i am running this file on terminal using command
php file.php
the 'ls' command is getting executed and 'redshift -O 4600' also getting executed.
But when i am executing this file on browser using url.
127.0.0.1/file.php
Only 'ls' command is getting executed. 'redshift -O 4600' is not getting executed. Is there some way through which i can execute such programs.
I have also tried other functions like exec etc.
It would not work as you are setting up your server at /var/www/html location which is used by public to access your computer through localhost. And since in localhost there is no such thing as redshift so it would run nothing and the operation would be executed and you will see no after effects. So, to run the system's command on localhost, you must run the server on any folder other than /var/www/html. So, to run the server on any other location, just go to that location and run
php -S localhost:8080.
I am using a php script on my apache/ubuntu server to call a bash script that triggers an application taking a python script as an argument (IDAPro).
PHP Code
chdir('/var/www/dashboard/team/static/sql');
$output = exec('sudo -u rohan ./start.sh');
Now, the above code works fine if I run the PHP file from the terminal - but only if I run it as the root user. Needless to say, if I execute the bash file directly it runs too.
But when I run the PHP file on the browser, it doesn't work and I get the following error in the apache error log:
QXcbConnection: Could not connect to display
Aborted
I understand that Apache/php runs as 'www-data' user (used the 'whoami' to verify), and that is why I have the sudo in my exec. I have tweaked and tinkered the permissions for both users to no avail. When I run the php file from the terminal as the 'www-data' user, it throws no error but does not do anything except display the random echo tags I at the start and end of the script to debug it.
I am a linux novice, so any help is greatly appreciated.
Okay, I finally managed to solve it.
The issue is not with the permissions, but it is with the environment variables.
I had to include the following line in my bash script
export DISPLAY=':0.0'
Note that setting the variable in the terminal and running the script does not work. The line needs to be inside the script.
I assume this is because the DISPLAY variable is not set if you run the script as any user other than root, which is what happens in case of Apache/PHP where the script is executed as the 'www-data' user.
perhaps you could use something like the following at the top of your script:
if [ "$(id -un)" != "rohan" ]; then
exec sudo -u rohan $0 "$#"
fi
export XAUTHORITY=/home/rohan/.Xauthority
export DISPLAY=:0
I have a PHP script, which accesses a database table, which contain information about some large files which need to be downloaded. I intend to run this PHP script as a cron job, in an hourly basis, and each time it runs, it should do the following things:
Check if there are files need to be downloaded
If there are, execute a shell script, which emits a wget command, and starts the downloading of the file, in the background, and when ready, runs a second php script, which updates the db tables of the completion of the download, and get back the process id of this shell script, for later use
Check if there are files currently being downloaded, if there are, check their process id is still active, and if not, adjust the table so we know that an error occured somewhere in the download
The shell script works accordingly, if I run it from the console, everything works fine, I am getting back the process id of the shell script also in my php file, my problem is, that when the originating php file exists, the shell script it initiated stops also.
Here's the code I use in php to start the shell script:
function runProcess($command, &$output=array()) {
$command = $command . ' > /dev/null 2>&1 & echo $!';
echo $command . "<BR>";
return exec($command, $output);
}
/** excerpt from the class that does the processing */
$pid=runProcess("sh ".self::DOWNLOAD_FILE_SHELL." ".DEFAULT_DIR_WHOME." 1.xml ".$this->Parent->XMLPath, $output));
echo $pid;
My question is as follows: How can I force the shell script to continue running, even when the parent process (the php script) exits?
If you're running on UNIX like environment, then the simplest way is to add ampersand (&) sign at the end of command line. this will tell the shell to run the command and not wait the completion. try this:
$command = $command . ' > /dev/null 2>&1 & echo $! &';
I have a python file that I would want to execute whenever a php page is called. The python file is in the same folder as the php file. The python script on execution edits a textfile that the php file accesses.
I have the following code:
<?php
exec("python somefile.py",$output);
$file = fopen("test.txt",'r');
....
For some reason, the python script never gets executed. I know this certainly as I can see it from the changes in the text file.
Since I was not sure if the script was made executable, so I included "python" on the command.
I also ran:
chmod +x somefile.py
just to make sure this was not the reason. But this did not help too.
What should I change to make the php page execute the python script whenever it is called?
This is most likely a permission issue.
Try
echo exec("whoami");
This will let you know who php is running as. Then you need to verify this user can run the python script. If the file was not created by the same daemon that runs python, you will most likely be denied permission.
Update
This will let you know who owns all the files you are working with. The file being written to needs to be writable by the user that is running python. If you are running python from ssh, that is most likely not the same user as when you run python from exec.
echo exec('whoami') . "<br>";
echo exec("ls -l test.txt") . "<br>";
echo exec("ls -l somefile.py") . "<br>";
Update 2
Because I constantly forget this exists.
passthru('python somefile.py 1 2>&1');
This will exec your python file, and output stderr to stdout.
I have a webpage on a linux NAS, with webserver and php running.
I want to start a script from de server, to write data to a database.
When i am on the new69.php (the website in question)
I have the
for example :
$uptime = exec('uptime');echo $uptime;
(and this works)
So i have a connection.
Then i do with a if statement:
if (isset($_POST['verzenden'])) {
$output1 = exec('whoami');
exec('/share/MD0_DATA/Qweb/sqlite/AllesNaardDB.sh');
} else {
echo "Nog lekker niets gedaan.";
Whoami in the is statememt gives the username "httpdusr",
So the if statement gets reached and works.
Now the exec statement when i do:
It has to execute the AllesNaardDB.sh
which contains:
cd /share/MD0_DATA/Qweb/sqlite
echo "Wasmachine"
python2.6 wasm_util_sql.py [param]
echo "alles gegeven"
exit 0
Nothing is shown or done.
But when i do:
$output = exec('/share/MD0_DATA/Qweb/AllesNaardDB.sh');
echo "<pre> $output </pre>";
I get the als line of the script in the variable $output,
"alles gegeven".
At first i tought that it could be something to do with the rights.
So i (switched user with) su to "httpdusr" and script in putty gets executed fine.
I also made another script ff.sh with "ls -l" to execute and everything works.
Also i treid
passthru(),
system(),
shell_exec.
The only improvements are that with system() and passthru(), the "echo "Wasmachine""
appeared on te website.
I don't get why the python jobs don't get started ?
I worked a little further, but could it be that there are some sqlite3 command which are user dependent ?
Like that i could execute some write action to the database as admin, but not as the httpdusr ?
try adding #!/bin/sh to the top of the script and make sure it's chmod a+x.
It is a prblem with a path in the python script, which is writing to the database.
It uses a different path when it is started from the webpage (MD0_DATA/Qweb/) then wen it is started from the shell script (MD0_DATA/Qweb/sqlite)
*i dit not solvend the problem yet, but understand it.
Everybody thanks for the effort.