首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在运行后台进程的bash脚本上调用shell_exec

在运行后台进程的bash脚本上调用shell_exec
EN

Stack Overflow用户
提问于 2016-09-02 14:21:32
回答 1查看 1.1K关注 0票数 0

我有一个bash脚本,它有几行代码,如下所示

代码语言:javascript
复制
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一开始就需要超时。有人能帮我个忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-02 15:34:05

测试这两个版本,您就可以得到它:

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占用10ish秒,test1.sh的工作方式与您的

return shell_exec("/path/to/bash/script arguments >/dev/null 2>&1");

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39294689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档