Koozali.org: home of the SME Server

Permissions for shell_exec in PHP

Offline beast

  • *
  • 245
  • +0/-0
Permissions for shell_exec in PHP
« on: February 24, 2019, 10:38:39 AM »
Can someone please explain to me why

Code: [Select]
php -r "echo shell_exec("ls");"
work fine on the command line but

Code: [Select]
php -r "echo shell_exec("ls /");"
does not?

I try to make a PHP script that will run as a cron job to execute backups etc.

E.g. echo shell_exec("/sbin/affa");"

But I am unable to make it work.

NB: I have tried to change PHPBaseDir

Thank you
Benny




« Last Edit: February 24, 2019, 10:45:20 AM by beast »

Offline beast

  • *
  • 245
  • +0/-0
Re: Permissions for shell_exec in PHP
« Reply #1 on: February 24, 2019, 10:51:05 AM »
Sorry made a mistake. The inner " need to be a '

Code: [Select]
php -r "echo shell_exec('ls /');"
Then it work on the command line but the problem is still there when run in a php file?


Offline beast

  • *
  • 245
  • +0/-0
Re: Permissions for shell_exec in PHP
« Reply #2 on: February 24, 2019, 10:56:48 AM »
This is the file I try to exec in a cron job.

The call

php -f backup.php

return right away with no action?

but

php -r "echo shell_exec('/sbin/affa --run prodserv');"

works fine?

Code: [Select]
<?php

// /home/e-smith/files/users/admin/home/backup.php

include_once("phpmailer/class.phpmailer.php");

function 
isCli() 

     
$sapiname php_sapi_name();
     
     if((
substr($sapiname 03) == 'cgi') || (substr($sapiname 03) == 'cli')) // PHP 4 and 5 version    
     
if(empty($_SERVER['REMOTE_ADDR'])) 
     {
          return 
true;
     } 
     
     return 
false;
}

function 
mailoutput($to$mailbody)
{
$mail = new PHPMailer();
$mail->IsMail();
$mail->SetLanguage("en");
$mail->IsHTML(true);
$mail->FromName "noreply@beast.dk"
$mail->From $mail->FromName;
$mail->Subject "Backup server log";
$mail->WordWrap 50;

$mail->MsgHTML($mailbody);
$mail->AltBody strip_tags(preg_replace("/<br>/""\r\n"$mailbody));
$mail->AddAddress($to);
if(!$mail->Send()) 
{
die("Mail Error: " $mail->ErrorInfo);
}
}

$parameters $_SERVER['argv'];

$mailbody "<h2>Backup server log</h2>";

//sleep(120);

$starttime time();

$returnstr shell_exec("/sbin/affa --run prodserv");

$execmin = (time() - $starttime)/60;

if(
strlen($returnstr))
  
$mailbody .= $returnstr;
else
  
$mailbody .= "<br>Backup performed to backupserver in ".$execmin." minutes";
  
$returnstr shell_exec("/sbin/affa --list-archives");

if(
strlen($returnstr))
  
$mailbody .= "<br><br>".nl2br($returnstr);
  
if(
isCli())
{
foreach($parameters as $key => $value
{
if($key 0
mailoutput($value$mailbody);

}
else
{
if(isset($_GET['email']))
mailoutput($_GET['email'], $mailbody);
}
  
//shell_exec("/sbin/shutdown -h +15");

?>

« Last Edit: February 24, 2019, 11:03:53 AM by beast »

Offline beast

  • *
  • 245
  • +0/-0
Re: Permissions for shell_exec in PHP
« Reply #3 on: February 24, 2019, 11:15:22 AM »
For some reason it seam to work now ?????

Sorry for the disturbance ;-)