我有一个bash脚本,其中有几行类似于以下内容
echo "Do something"
/bin/sh -c 'echo $$>pidfile && exec "command"' &
echo "Ran Command">/path/to/outputfile.txt
exit 0
然后我从PHP脚本中调用它
return shell_exec("/path/to/bash/script arguments");
现在,当我这样做时,命令将成功运行,并且outputfile.txt包含“ Ran命令”。
但是,PHP脚本会在10秒钟后超时。bash脚本运行大约需要2-3秒
如果我将行更改为 return shell_exec("/path/to/bash/script arguments >/dev/null 2>&1");
然后执行,PHP脚本不会超时。
我知道为什么重定向输出可以使PHP继续执行,但是我不知道为什么PHP首先要超时以要求执行此操作。有人可以给我一些帮助吗?
测试这两个版本,您会得到:
test1.sh /bin/sh -c 'sleep 10' >/dev/null 2>&1 &
test2.sh /bin/sh -c 'sleep 10' &
用php在命令行上都运行
test1.php <?php shell_exec('test1.sh');
test2.php <?php shell_exec('test2.sh');
并看到差异。
test2.sh需要10秒钟的时间,而test1.sh的工作方式就像您的
return shell_exec("/path/to/bash/script arguments >/dev/null 2>&1");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句