修改正在运行的脚本并重新加载它而不杀死它(php)

发布于 2024-11-08 03:38:05 字数 418 浏览 4 评论 0原文

我有一个在 Debian 上运行的游戏服务器,玩家可以通过网络编辑(已经运行的)php 脚本来修改游戏。但是,一旦对脚本进行更改并保存,更改的影响只有在脚本被终止并重新启动后才会发生(我必须在终端中手动执行此操作)。如果不向用户授予 shell 访问权限,脚本如何知道在发生更改后重新加载自身的新版本?该脚本正在 GNU 屏幕中运行。

虽然我对 GNU screen、php 和 linux 命令的总体了解有限,但我认为必须有一种方法可以做到这一点。

最简单的方法是什么?

编辑 澄清一下,人们修改的脚本是通常读取服务器输出日志的基本脚本。因此,当脚本看到“PLAYER_DIED”时,它会写入一个文件,该文件又被服务器读取并执行一些操作,例如生成一个区域。 人们现在使用链接到 php 源代码的基于 Web 的基本文本编辑器来编辑此脚本

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-11-15 03:38:05

有多种方法可以实现此目的,但很难判断哪种方法最好,因为您不共享任何源代码。

为什么不在您用来让玩家修改脚本的同一个脚本中重新启动它呢?

另一个解决方案是使用一个小的 cron 脚本,每分钟运行一次以检查文件是否已更改。如果是这样,它将重新启动实例。在最坏的情况下,玩家必须等待一分钟才能看到变化。

另外我想知道您是否正在使用某种守护进程来运行由玩家编辑的实际脚本,或者您是否直接运行该脚本。

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.

倾城月光淡如水﹏ 2024-11-15 03:38:05

在黑暗中拍摄..但似乎您需要使用 PHP 的进程控制功能来终止脚本并在知道脚本已更改后再次运行它。我还没有测试过这个(根本),所以要持保留态度:

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

使用 pcntl_* 函数:PHP 需要作为 CGI 运行,并且需要使用 --enable-pcntl 编译 php。既然你说你拥有硬件,我想这不应该是一个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文