1

I simply want to restart named depending on whether a file exists. I've been stuck on this all day.

Command to create bash file:

$this->execute('echo -e "#!/bin/bash\nsudo /sbin/service named reload" >> /var/reload_named.sh');

Here is my cronjob:

*/1 * * * * cronjob: sudo sh /var/reload_named.sh; rm -f /var/reload_named.sh;

Here is what happens when the cronjob runs (/var/log/cron):

Jul 30 18:34:01 digitalocean CROND[24864]: (root) CMD (cronjob: sudo sh /var/reload_named.sh; rm -f /var/reload_named.sh )
Jul 30 18:34:01 digitalocean CROND[24862]: (root) UNSAFE (”example@digitalocean.com”)

For some reason it says it is UNSAFE. I've tried running with and without sudo.

It manages to delete the file but not restart named. I have tried doing so many other methods to get this to work.

I've tried (Over lots of Googling):

  • Running exec('service named restart') in php
  • Creating a .c file and adding a user that runs it from php
  • Running service named restart directly in crontab -e
  • Attempted different variations on running it with sudo
  • Tried adding apache user to sudo (Still fails)

Any help much appeciated

(I am on Centos 6.7)

Jack
  • 3,271
  • 11
  • 48
  • 57
  • You create a script writable as unprivileged user and run it via `sudo` with privileged permissions? That of course is unsafe. – Pinke Helga Jul 30 '16 at 18:51
  • I cannot get it to run at all. What needs to change in order to restart named from a php script? I feel as if I've exhausted everything. Surely this should be a simple thing to do. – Jack Jul 30 '16 at 18:53
  • If have administrative (sudo) permissions, you should also have permissions to run (configure) `exec` / `system` / `passthru` from PHP. Could make things easier. http://stackoverflow.com/questions/732832/php-exec-vs-system-vs-passthru – Pinke Helga Jul 30 '16 at 18:59
  • Thank you, I have tried all but that passthru one but still seems to fail. :/ – Jack Jul 30 '16 at 19:03

1 Answers1

0

I finally worked out a way to do this. Here is a method which SSHs into itself as root and runs service command:

$this->root_execute('service named reload');

    public function root_execute($command = '')
    {
        set_include_path('/path/to/dir/ssh/');
        require_once('Net/SSH2.php');

        $ssh = new Net_SSH2(SSH_HOST);

        if (!$ssh->login(SSH_USER, SSH_PASS)) {
            exit('failed');
        }
        $res = $ssh->exec($command);
        $ssh = null;
        restore_include_path();
        return $res;
    }

(Unfortunately doesn't work with HTTPD if running from http .php)

Jack
  • 3,271
  • 11
  • 48
  • 57