3

I'm running an Apache server on CentOS and would like to be able restart the webserver from a protected page using the following:

PHP:

<?php
ignore_user_abort(true);
shell_exec('sh sh/restart.sh');
?>

restart.sh:

service httpd restart

My question is if the web server shuts down and the PHP stops executing will the sh script continue running to bring the web server back online?

3
  • 1
    what happened when you tried it?
    – user557846
    Nov 4, 2012 at 19:12
  • 1
    Running service httpd restart requires root, doesn't it? I hope your PHP pages aren't running with root permission.
    – Wyzard
    Nov 4, 2012 at 19:14
  • The PAM package might allow him to authenticate as root to do this. pecl.php.net/package/PAM Nov 4, 2012 at 19:25

3 Answers 3

0

You should be fine since Apache doesn't shutdown until after the command is issued. But if you really wanted to be safe, use nohup:

shell_exec('nohup sh sh/restart.sh');
0

If your PHP runs as apache module, then once you kill httpd your script will be terminated instantly. So you need to delegate restart to i.e. command line script (called using exec() or shell_exec())

0

You might be able to add an & at the end of the command. This will fork the process and run it in the background. This way it will not depend on apache still running.

shell_exec('sh sh/restart.sh &');

If this works, you should not need ignore_user_abort().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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