duanpie4763 2012-03-29 20:49
浏览 567

php shell_exec()帮助在后台运行脚本

My project calls for 3 php scripts that are run with if-else conditions. The first script is loaded on the index page of the site to check if a condition is set, and if it is, it calls for the second script. The second script check to see if other conditions are set and it finally calls for the last script if everything is good.

Now I could do this by just including the scripts in the if statement, but since the final result is a resource hogging MySQL dump, i need it to be run independently of the original trigger page.

Also those scripts should continue doing their things once triggered, regardless of the user actions on the index page.

One last thing: it should be able to run on win and nix.

How would you do this?

Does the following code make any sense?

if ($blah != $blah-size){
shell_exec ('php first-script.php > /dev/null 2>/dev/null &'); 
 }
//If the size matches, die
else {
 }

Thanks a million in advance.

UPDATE: just in case someone else is going through the same deal. There seem to be a bug in php when running scripts as cgi but command line in Apache works with all the versions I've tested.

See the bug https://bugs.php.net/bug.php?id=11430

so instead i call the script like this:

exec("php-cli mybigfile.php > /dev/null 2>/dev/null &"); 

Or you could call it as shell. It works on nix systems but my local windows is hopeless so if anyone run it on windows and it works, please update this.

  • 写回答

2条回答 默认 最新

  • douzhenzu0247 2012-03-29 20:53
    关注

    The easiest way I can think is that you can do

    exec("screen -d -m php long-running-script.php");

    and then it will return immediately and run in the background. screen will allow you to connect to it and see what's happening.

    You can also do what you're doing with 'nohup php long-running-script.php', or by writing a simple C app that does daemonize() and then execs your script.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大