Modifying a running script and having it reload without killing it (php)

拈花ヽ惹草 提交于 2019-12-11 08:05:49

问题


I have a gameserver running on Debian where players can edit an (already-running) php script via web to modify the game. However, once changes are made to the script and saved, the affects of the changes only happen once the script is killed and rebooted (I have to do this manually in terminal). Without giving shell access to users, how can the script know to reload a new version of itself once changes have been made? The script is running in a GNU Screen.

Although my overall knowledge on GNU screen, php, and linux commands are limited, I think there has to be a way for this to be done.

What would the easiest way be?

EDIT To clarify, the script that people modify is a basic script that usually reads a server output log. So when the script sees "PLAYER_DIED" it writes to a file, which in turn is read by the server and does some stuff, like spawn a zone. People edit this script right now with a basic web-based text editor linked to the php source code


回答1:


There are numerous ways to achieve this, but it's hard to tell which method is the best since you don't share any of your source code.

Why not restart it within the same script that you use to let the players modify the script?

Another solution is to have a small cron-script that runs every minute to check if the file was changed. If so it will then restart the instance. In a worst case scenario, the players have to wait a minute until the changes are seen.

Also I'm wondering if you are using some kind of deamon that is running the actual script that is edited by the players or if you are running that script directly.




回答2:


Shooting in the dark here.. but it seems like you will need to use PHP's process control functions to terminate the script and run it again once you know the script has changed. I have not tested this (at all), so take it with a grain of salt:

// signal handler function
function sig_handler($signo)
{

     switch ($signo) {            
         case SIGHUP:
             // Asked to restart. I guess you will need to call `exec` to start a new instance before terminating

             break;
         default:
             // handle all other signals
     }

}

// setup signal handler
pcntl_signal(SIGHUP,  "sig_handler");

// Send restart signal to self (after you detect the script was modified):
posix_kill(posix_getpid(), SIGHUP);

There are limitations of using pcntl_* functions : PHP needs to be run as a CGI and you need to compile php with --enable-pcntl. Since you said you own the hardware, I guess this shouldn't be an issue.



来源:https://stackoverflow.com/questions/6068359/modifying-a-running-script-and-having-it-reload-without-killing-it-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!