-1

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

Gloytos htyqo
  • 345
  • 1
  • 3
  • 12

2 Answers2

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

1

this will fix ur problem

$output = shell_exec('sudo /usr/bin/sh  /var/www/1.sh > /dev/null 2>&1');
arlind
  • 165
  • 1
  • 3
  • 15