Executing sudo shell script by php

1.2k Views Asked by At

i am trying to execute pkill command by php script.

killengine.sh script runs fine from terminal, both with ./killengine.sh and php restart.php.

this is killengine.sh

#!/bin/bash
sudo pkill -f  engine

and this is restart.php

$out = shell_exec("/var/www/killengine.sh 2>&1");
var_dump($out);

Both files have 755 permissions. Ownew of restart.php is apache, and root is owner of killengine.sh. Also i tried with both owners to be root/apache.

In visudo i made this changes:

Defaults:apache !requiretty

but i get: "sudo: no tty present and no askpass program specified"

Then i tried with

Defaults!/var/www/killengine.sh !requiretty

then i get: "sudo: sorry, you must have a tty to run sudo"

Also, this line is present all the time at the EOF

apache ALL=NOPASSWD: /var/www/killengine.sh

but without success.

OS is Centos 6

Any ideas?

1

There are 1 best solutions below

0
Aleks On

solution:

changed restart.php to

$out= @shell_exec("sudo /var/www/killengine.sh");