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


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 &");

  • 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

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:


    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);

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:

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

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

  • 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 .