0

I am trying learn to execute shell scripts from within PHP code. So, I made a test program to execute a bash script from within PHP. However, it has no effect. The relevant code is shown below.

<?php 
.......
shell_exec('/bin/bash /var/www/html/just_touch.sh');
?> 

The just_touch.sh script just creates a new file, like as shown below.

touch /home/user/some.txt

I was expecting to have file /home/user/some.txt after execution, but no, it isn't made. What mistake, am I doing?

P.S: The following code works though.

$output = shell_exec('ls /home/user');
echo $output;

Does this have anything to do with permissions?

Moreover, I notice that while this prints "Can you see me?".

$output = shell_exec('echo Can you see me?');
echo $output;

This doesn't!

shell_exec('echo Can you see me?')

What is going on here?

5
  • 1
    Give the full path to your /somedir/just_touch.sh Commented Jul 18, 2016 at 16:13
  • It still doesn't work. Is there a way to see what error occurs from Mozilla? Commented Jul 18, 2016 at 16:14
  • To see something in Mozilla use passthru() instead of shell_exec(). php.net/manual/en/function.passthru.php Commented Jul 18, 2016 at 16:15
  • Most likelely /home/user/some.txtis not writable by webservers userid Commented Jul 18, 2016 at 16:50
  • How can I make that writable then? How can I even check if its writable or not through webservers userid? Commented Jul 18, 2016 at 16:53

1 Answer 1

3

Stderr is lost when using shell_exec. You might wan't to use:

shell_exec('/bin/bash /var/www/html/just_touch.sh 2>&1');
Sign up to request clarification or add additional context in comments.

3 Comments

Very useful indeed, because now I see the message, touch: cannot touch '/home/user/some.txt': Permission denied. These permission rights are killing me though :(! Do, you know anyway to solve these permission rights?
Make the target directory /home/user writable by the webserver user. I.e make it group-writable: chmod g+w /home/user and set the group of the home directory to the same group the webserver is running: chgrp wwwrun /home/user
I see. At the moment I got away by setting permissions to 777. But I think that would be very insecure. I'll checkout with your approach.

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.