Execute Linux command in PHP gives error "sudo: no tty present and no askpass program specified"

1

I'm trying to restart a Linux service, but it does not work for me.

I have this piece of code taken from the web:

<form action="reset.php" method="post">
    <input type="submit" value="Reiniciar" class="button" >
</form>

And this is the reset.php :

<?php

$output = shell_exec("service garen restart");

echo "<pre>$output</pre>";
?>

When I give the button to execute reset.php , it goes blank and does not execute the command.

If I add sudo to the command it does not work either and the log indicates:

  

sudo: no tty present and no askpass program specified

    
asked by Fran 30.05.2017 в 10:12
source

1 answer

3

The system is telling you:

  

sudo: no tty present and no askpass program specified

This means that sudo wants to show you a prompt where you can ask for the password ... but it has no where to do it because it is not running in an environment where it exists!

Therefore, let's go to the origin of the problem: the user that you want to execute the sudo service garen restart command does not have the permissions to do it or at least directly without a password, hence the request.
What you have to do is give permissions in the sudoers and do it in a way that does not ask for the password.

Assuming that you are working with Apache and your user is apache , you should open the sudoers with visudo and add a line of the type:

apache ALL = NOPASSWD: /usr/sbin/service garen restart

Where /usr/sbin/service may vary depending on your system as it is the full path of service . To get it, type which service .

    
answered by 30.05.2017 / 10:57
source