pythonic
pythonic

Reputation: 21615

PHP shell_exec command doesn't work

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?

Upvotes: 0

Views: 5720

Answers (1)

J&#252;rgen H&#246;tzel
J&#252;rgen H&#246;tzel

Reputation: 19727

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');

Upvotes: 3

Related Questions