-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

2
  • So what does your php error log file say wht is happening? Commented Oct 27, 2018 at 14:49
  • Possible duplicate of Run Bash Command from PHP Commented Oct 27, 2018 at 15:00

2 Answers 2

1

this will fix ur problem

$output = shell_exec('sudo /usr/bin/sh  /var/www/1.sh > /dev/null 2>&1');
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

is not writing to test1.txt thats the problem
I think trouble is: you are do not define path to test1.txt: echo "test" > /var/www/TEST1.txt
defined path but is same problem. if u run from terminal ./1.sh it create text1.txt but from php if i try to run 1.sh text1.txt is not created
Okey. May be user www-data (apache2 server) don't have a write permission to the /var/www (or where you are save test1.txt) directory?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.