0

Ok, this is a tricky one (I think) I need to reload php5-fpm from a php script The obvious issue is that once the reload command is executed, the current script is disturbed, I tried to delay the execution like so

(sleep 5;  sudo service php5-php reload) & echo ""

but this does not help, the immediate nginx response is 502 bad gateway. FYI, i'm using php's exec function

any way around this?

3 Answers 3

1

Make sure the user running php has the sudo privileges to reload php5-fpm, then:

shell_exec("nohup sh -c 'sleep 1; sudo reload php5-fpm' > /dev/null 2>&1 &");

1
  • This indeed works to properly restart not just for PHP-FPM but Nginx as well (which would otherwise see the request fail). Kudos for considering nohup. Ingenious!
    – fevangelou
    Mar 17, 2021 at 15:16
0

Try putting the restart command in a shell script, starting with the sleep period. So your php function calls the script on the server, rather than restarting php directly. This way it cannot kill itself.

Also check this answer to see how it can be done directly:

https://stackoverflow.com/questions/9798438/automatically-restart-php-script-on-exit

<?php
    echo ++$argv[1];     // count every restart
    $_ = $_SERVER['_'];  // or full path to php binary

    echo "\n======== start =========\n";
    // do a lot of stuff
    $cnt = 0;
    while( $cnt++ < 10000000 ){}
    echo "\n========== end =========\n";

    // restart myself
    pcntl_exec($_, $argv);
?>
0

Would be nice to see some of your php code; what are you trying to achieve with echo ""?

Anyway, you could try it with "sh -c" and "disown", like so:

<?php
    exec("sudo sh -c 'sleep 3 && service php5-fpm restart' >/dev/null 2>&1 & disown");
    echo("done");
?>

"sudo" with exec() will only work, if you put www-data in the sudoers' file, which is quite a security issue. Are you sure?

1
  • Now what? Did you try it, phper? Apr 18, 2016 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .