dongtong7990 2014-09-17 15:05
浏览 89
已采纳

PHP命令行脚本是否可以更新自身并使用新代码重新启动?

Many moons ago when I was getting started in programming, I coded a MUD (Multi User Dungeon - text based 'MMO' for back before the internet had pictures, for all you youngins!). It was written in C and I learned a ton, and one of the coolest things I learned while coding that mud was making the individual areas of code compiled as linked libraries (I believe thats what they are called? Its been a long time since I coded in C) and I was able to dynamically update/change the code of the game without ever having to shut it down/reboot it. Unlink the old library, move the new one in place, relink, turn that functionality back on.

Present day: I have a PHP command line script I have running that handles some server tasks. Before the lecture, yes, I know it's not the best/most efficient language to do this in. But due to certain factors, it's the language its done in right now.

The script I'm running actually runs on 4 different servers, and when it starts, there is a calling script that runs a wget to get the updated 'core' code from the main server, includes it, then runs it. Here's the code:

#!/usr/bin/php -q
<?php
  echo "Grabbing latest code...
";
  `wget -N http://blahblahblah.php`;
  `wget -N http://blahblahothercode.php`;

  include('blahblahblah.php');
?>

So my question - is it possible to, while it's running, have it update it's own code and reload itself? My current solution is for it to exit() after using a shell 'at' command to restart itself. Clunky, but it could accomplish the goal.

The problem there is when the script starts, it grabs an encryption key from stdin that must be typed in when the script starts. I don't ever want that key saved to disk anywhere (I don't want the key saved to a temp file to be read back in, or in any command history for 'at'). IDEALLY, I'd like to leave the key in memory and just 'replace' the running code. If thats even possible with PHP.

When we eventually redo this in C++ we have the option of libraries, but for now, is there any way to accomplish that in PHP?

Just had a thought right before I hit submit... when PHP hits an include command, does it do the include once when it runs, or will it run the include every time it sees it? That might be an option if I loop around an include...?

  • 写回答

3条回答 默认 最新

  • dongyan1936 2014-09-18 09:39
    关注

    As per the comment (copy/pasting):

    There is only 1 way, and it's called process supervising. You fork, having your parent process contain that info about the encr. key (in a variable). Now the fun part - the child is the running code. Now what you do - you need to detect changes on files (event loop, stat() with polling - whatever you can use). Once you detect the change occurred in the child process, it kills itself (exit, die, whatever). Now we're back to parent - parent receives the signal that its child exited. You boot it back again (yes, fork again). Repeat forever. However, your key is in memory and can be looked up so.

    Update: I've been using this approach for a while, however I also use LibEV and PHP extension to work with it in order to react to stat events (file changes). You can take a look at the extension here: https://github.com/m4rw3r/php-libev

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分