如何使用php在后台运行程序(非阻塞)? [英] How can I run a program in the background (non blocking) with php?

查看:242
本文介绍了如何使用php在后台运行程序(非阻塞)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php中运行一个shell脚本,但是这个shell脚本需要花费很长时间才能执行(它有睡眠),我不希望Web服务器在执行此脚本时阻塞. 我在php中尝试了exec()shell_exec(),但是服务器停止运行,直到shell脚本完成!

I want to run a shell script in php, but this shell script takes a long time to execute (it has sleep in it), I don't want the web server to block when executing this script. I tried exec() and shell_exec() in php but the server stops until the shell script finishes!

我考虑过在shell脚本本身中执行fork,但是我不知道该怎么做.我只希望php脚本调用此shell脚本并继续工作,我不等待该脚本的任何结果.

I thought of doing fork in the shell script itself but I don't know how to do that. I just want the php script to call this shell script and continue working, I'm not waiting any result from the script.

我尝试使用&在后台运行shell脚本,但仍然会阻塞:( 任何帮助都将受到高度赞赏.

I tried running the shell script in the background with & still get blocking :( Any help is highly appreciated.

推荐答案

我通过添加

"> /dev/null 2>/dev/null &"

在脚本名称后为shell_exec() php函数

after the script name to the shell_exec() php function

谢谢

这篇关于如何使用php在后台运行程序(非阻塞)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆