0

I am making a tool that is interacting with NAGIOS where some config files should be added so a restart will be needed.

What I need to know is how to restart NAGIOS using PHP code since this tool is written in PHP .. when I try to do this using:

  shell_exec("service nagios restart");

changes do not take place but when I do this manually by the console all changes I did using the PHP script are applied ... after 2 minutes research I found that I am asking linux to execut this command while I am logged in as apache user so I changed the command to:

  shell_exec('echo "mypass" | sudo -S service nagios restart');

still having the same problem ... new config files are not read until I restart manually

any suggestions will be appreciated :)

  • What is returned by shell_exec ? $output = shell_exec("service nagios restart"); echo "<pre>$output</pre>"; – Iain Apr 10 '12 at 10:24
  • @lain it returns what should be returned as if I'm restarting manually :S – Ahmad Hajjar Apr 10 '12 at 12:46
  • Running configuration check...done. Stopping nagios: .done. Starting nagios: done – Ahmad Hajjar Apr 10 '12 at 12:50
  • FYI: You should not restart nagios when the conf changes; just 'reload' is fine. (e.g. SIGHUP) – Keith Apr 11 '12 at 16:40
  • @Keith how to do this reload ?? I mean in PHP ?? – Ahmad Hajjar Apr 12 '12 at 9:40
1

It will be better to add your web server to sudoers to enable it from restarting nagios service without being prompted for a password. This will be safer than typing the password in a PHP file. To so it, type:

$ sudo visudo

Then, add a line lik this:

apache_user ALL=NOPASSWD: /path/to/script

This will enable apache_user to execute only /path/to/script with sudo privileges with being prompted for a password.

  • I did that and nothing changed ... when I execute this command in PHP echo shell_exec('echo "nagiosxi" | sudo -S service nagios restart'); it echos nothing – Ahmad Hajjar Apr 10 '12 at 13:15
  • If you added apache user to sudoers file, you need to execute it like: shell_exec("sudo service nagios restart");. No need for echo password part. – Khaled Apr 10 '12 at 13:26
  • echo shell_exec('sudo service nagios restart'); is not printing anything as well :( – Ahmad Hajjar Apr 10 '12 at 13:34
  • The line you added to the sudoers file had the proper path to the service command? Is the user correct? Could you post the line you added? – Nupraptor Apr 10 '12 at 18:51
  • I added this line : apache ALL=NOPASSWD: /sbin/service – Ahmad Hajjar Apr 11 '12 at 7:49
0

My guess is that there is still something wrong with the permissions, log in as the apache user with su apacheusername and then execute your command

echo "nagiosxi" | sudo -S service nagios restart

to verify this does not give any errors. Are you sure that you are running Nagios as a service?

  • when I try to execute this command su apache it gives me : this account is currently not available – Ahmad Hajjar Apr 10 '12 at 13:02
  • but is apache your webserver user? In Debian it's www-data. – Lucas Kauffman Apr 10 '12 at 13:32
  • yes it is ... when I execute whoami in shell_exec in PHP it gives me apache – Ahmad Hajjar Apr 10 '12 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.