场景:
- 我有一个PHP脚本(通过web访问),它生成一个shell脚本,即使在请求结束后也需要继续运行
- 我需要将输出发送到磁盘上的文件(即"./script.sh>run.log")
我尝试的每一种方法似乎都会导致PHP仍在等待shell脚本完成执行。我尝试过的方法:
nohup ./script.sh > run.log &
nohup ./script.sh > run.log 2>&1
nohup ./script.sh > run.log 2>&1 &
./script.sh &
./script.sh > run.log 2>&1
./script.sh > run.log &
我想我可能遗漏了一些很明显的东西。有什么想法吗?
我认为您可以通过使用以下脚本启动screen
实例来实现这一点:
screen -d -m 'bash ./script.sh > run.log'
你的数字5方法很接近,但你需要将它与6结合起来,就像这个一样
shell_exec("/script.sh > run.log 2>&1 &");
或者像这样重定向到/dev/null
shell_exec("/script.sh > /dev/null 2>/dev/null &")
PHP开发服务器似乎无法正确地派生命令使用像Apache或NGinx这样的真实web服务器可以解决这个问题。
您尝试过shell_exec吗?
您可能对这篇文章感兴趣:有没有一种方法可以在不等待命令完成的情况下使用shell_exec?