from PHP I can run commands with shell_exec but can't run bash files
I run this command
sudo ls /var/www/
and i get results
/var/www/1.sh
/var/www/2.sh
/var/www/3.sh
but when I run this command nothing happens
$output = shell_exec('sudo sh /var/www/1.sh > /dev/null 2>&1');
echo "<pre>$output</pre>";
In 1.sh i added this code
#!/bin/bash
/usr/bin/echo "test" > TEST1.txt
it works when i type in terminal ./1.sh so only from php is not working
Server: centos
from PHP I think I have root premission to execute commands
this will fix ur problem
$output = shell_exec('sudo /usr/bin/sh /var/www/1.sh > /dev/null 2>&1');
Your bash script has output into the TEST1.txt file, not into console!
Try that:
#!/bin/bash
/usr/bin/echo "test"
Or like that
$output = shell_exec('sudo sh /var/www/1.sh 2>&1');
echo "<pre>" .file_get_contents('/var/www/TEST1.txt'). "</pre>";
That string are wrong!!!!
$output = shell_exec('sudo sh /var/www/1.sh 2>&1');
&1 don't have a reason, because don't have definition
$output = shell_exec('sudo sh /var/www/1.sh > /dev/null 2>&1');
In this case &1 === /dev/null
Related
I want my PHP script can mont a remote system with sshfs command.
But it doesn't seem to work, the folder has been created but the folder still empty after execution. I also tried with a user without SU and it was working fine.
mkdir ("/var/mont/remote/");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;
This script (test.sh) should work :
#!/usr/bin/env bash
php -r 'mkdir ("/var/mont/remote");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;'
Run it as bash test.sh
I have main php script on web-server nginx+php-fpm and try to run another php script in background using GET request from web-browser. Line code in main.php to call detect.php:
exec("/usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 &");
detect.php does not start. I don't have any errors.
If to remove "&":
exec("/usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 ");
detect.php starts successfully.
From shell bash with "&" :
sudo -u www-data /usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 &
Script detect.php starts successfully.
try this and make sure your php path are correct
$dir=dirname(__FILE__);
$file_name="detect.php";
$php_path="/usr/bin/php";
$args = array(6);
$command1="cd $dir";
$command2="$php_path $file_name ".implode(" ",$args) ." > /dev/null 2>/dev/null &";
$final_command=$command1."; ".$command2;
shell_exec($final_command);
I am trying to execute a command with exec() and redirecting stdout and stderr to a file.
exec("nohup python main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 &");
It will create the file but it will not print anything to it.
If I run the command in a terminal everything outputs without a problem.
Got it working. Python does its own output buffering which kept it from writing to the file. Running it with the -u option disables this. Final code looks like this:
exec("nohup python -u main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 </dev/null &");
Thanks.
I created a .sh file in my linux and want to run it from webpage (php + Apache).
Some of the simple example execute without problem. But I can't run with echo Pipe:
#!/bin/sh
set +v
cp /tmp/test/test1.tar.gz.gpg /tmp/test/ts2.gpg
echo 'myPassword' | /usr/bin/gpg --passphrase-fd 0 --output /tmp/test/test1.tar.gz --decrypt /var/backups/test1.tar.gz.gpg
exit 0
It can copy ts2.gpg but can't run the second command (decrypt).
You want shell_exec.
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
Enjoy
I have a QNAP box, that runs a flavor of linux and I am having problems getting the PID of a process using a php script. What I have so far:
$command = "PATH=$PATH:/share/MD0_DATA/.qpkg/Optware/bin: nohup /opt/bin/plowdown -o /share/MD0_DATA/Qdownload/plowshare http://www.megaupload.com/?d=m7duotr1 2> /share/MD0_DATA/Qdownload/plowshare/outputeeds.txt > /dev/null &";
exec($command, $out);
$result = $out[0];
echo $result;
If I run the command through PUTTY, I get:
[~] # nohup /opt/bin/plowdown -o /share/MD0_DATA/Qdownload/plowshare http://www.megaupload.com/?d=m7duotr1 2> /share/MD0_DATA/Qdownload/plowshare/outputteeds.txt > /dev/null &
22526
What am I doing wrong?
Thanks,
Cristian.
The shell does not normally print the PID of a process it starts in background, unless it's interactive. Otherwise, you would get tons of output during bootup just from the PIDs of all the processes that get started.
So you need to make the shell print the PID. Do
exec("(PATH=$PATH:/share/MD0_DATA/.qpkg/Optware/bin: " .
"nohup /opt/bin/plowdown -o /share/MD0_DATA/Qdownload/plowshare " .
"http://www.megaupload.com/?d=m7duotr1 2> " .
"/share/MD0_DATA/Qdownload/plowshare/outputeeds.txt > /dev/null &);" .
"echo $$;", $out);
http://nl2.php.net/manual/en/function.getmypid.php