Jump to content

Execute shell script in PHP


Go to solution Solved by requinix,

Recommended Posts

:facewall:  OK I am banging my head here and it hurts... :facewall:

 

I am trying to execute a shell script that I wrote and it is being a PITA

<?php
// for future use
$end = $_REQUEST['end'];
$start = $_REQUEST['start'];

// This DOES NOT WORK - pdfit.sh has temporary permissions 777
$output = shell_exec('/home/webserver/mysite/public_html/commissions/wkhtmltox/bin/pdfit.sh');
echo "<pre>$output</pre>";

// This WORKS GREAT
$output = shell_exec('ln -a');
echo "<pre>$output</pre>";
?>

So I know I am able to use shell_exec as in the second example shown, the pdfit.sh script works great from the CL so I am at a total loss.

 

Chad

Link to comment
https://forums.phpfreaks.com/topic/304838-execute-shell-script-in-php/
Share on other sites

What do you get as output for your script? An error? Nothing? Try adding stderr redirection.

$output = shell_exec('/home/webserver/mysite/public_html/commissions/wkhtmltox/bin/pdfit.sh 2>&1');
Also keep in mind that the environment will be difference when the server runs your script compared to when you run it from a terminal. Notable the PATH variable may be different and prevent programs from being found. Use absolute paths to your programs inside your shell script or setup PATH as needed.

That gave me the answer I needed.  Apache has no permission to write there so even though I applied 777 permissions to the script when run by apache it cant write the resulting file to the directory it resides in.  I have had nothing but terrible luck with the whole permissions for my user webserver and apache running as www-data.  I have followed several tutorials in this topic all to no avail.  Nothing seems to give apache the rights to make files in the system.  Short of 777 the entire box lol (NO I WILL NOT DO THAT!) I am at a loss.  Thank you for your assistance!!!

 

Funny how all I have done is "hack" many scripts together over the years and I am still a total noob and yet the forum calls me an "Advanced Member" ::)

Edited by chadrt
  • Solution

0777 on the script means anyone can read, write, or execute the script.

 

If you're absolutely sure that the script is safe to execute - no vulnerabilities - then you can setuid it to cause it to be run in its owner's account instead of Apache. But there's no guarantee the system will honor that.

 

Otherwise it would be great if you could change things so that the directory it needs to write to is also owned by Apache. Means your personal account couldn't change anything inside without sudo but the script could run without fiddling with permissions.

 

Worst case, set 0777 on the directory so that anyone can read the directory, write new files in it, or traverse the directory.

 

I am still a total noob and yet the forum calls me an "Advanced Member" ::)

It says what? ;)
  • Like 1

It says what? ;)

 

That is freaking awesome! 

 

 

0777 on the script means anyone can read, write, or execute the script.

 

If you're absolutely sure that the script is safe to execute - no vulnerabilities - then you can setuid it to cause it to be run in its owner's account instead of Apache. But there's no guarantee the system will honor that.

 

Otherwise it would be great if you could change things so that the directory it needs to write to is also owned by Apache. Means your personal account couldn't change anything inside without sudo but the script could run without fiddling with permissions.

 

Worst case, set 0777 on the directory so that anyone can read the directory, write new files in it, or traverse the directory.

 

So I own the box its in my house as my little playground with one of my static ip's.  I am not worried anyone else on the box with abilities.  With that being said I have tried changing the directory ownership to www-data:www-data and 777 on the script, that does not work I only get this:

Loading pages (1/6)[>                                                           ] 0%[======>                                                     ] 10%[==================>                                         ] 30%[============================================================] 100%QPainter::begin(): Returned falseError: Unable to write to destination                              Exit with code 1, due to unknown error.

OK so I was using absolutes everywhere except the file output location in my shell script.  Now that I have that out of the way I can go hang my head in a corner :  Everything is working as expected!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.