如何在 PHP 中同步执行 shell 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7621348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute shell commands synchronously in PHP
提问by Rahul
I need to run multiple scripts(5 scripts) via cmd, I want to make sure unless and until the first script finishes the second should not initiate. Thus after first script completes then only second should being then third one and so on.. Currently I am using the following code to do this
我需要通过 cmd 运行多个脚本(5 个脚本),我想确保除非第一个脚本完成,否则第二个脚本不应该启动。因此,在第一个脚本完成后,只有第二个应该是第三个,依此类推。目前我正在使用以下代码来执行此操作
exec ("php phpscript1.php ");
exec ("php phpscript2.php ");
exec ("php phpscript3.php ");
exec ("php phpscript4.php ");
exec ("php phpscript5.php ");
I think these scripts run asynchronously, any suggestion guys so that these scripts can be run synchronously.
我认为这些脚本是异步运行的,任何建议都可以让这些脚本同步运行。
回答by stivlo
回答by Dennis
If I'm getting you right, you're executing php scripts from inside a php script.
如果我说得对,您就是在 php 脚本中执行 php 脚本。
Normally, php waits for the execution of the exec ("php phpscript1.php");
to finish before processing the next line.
通常,phpexec ("php phpscript1.php");
在处理下一行之前等待 的执行完成。
To avoid this, just redirect the output to /dev/null
or a file and run it in background.
为避免这种情况,只需将输出重定向到/dev/null
或 文件并在后台运行它。
For example: exec ("php phpscript1.php >/dev/null 2>&1 &");
.
例如:exec ("php phpscript1.php >/dev/null 2>&1 &");
。
回答by Daniel Lopes
Check out the execfunction syntax on php.net.
You will see that exec
does not run anything asynchronously by default.
查看php.net 上的exec函数语法。您将看到exec
默认情况下不会异步运行任何东西。
exec
has two other parameters. The third one, return_var
can give you a hint if the script ran successfully or any exception was fired. You can use that variable to check if you can run the succeeding scripts.
exec
有另外两个参数。第三个,return_var
如果脚本运行成功或任何异常被触发,可以给你一个提示。您可以使用该变量来检查是否可以运行后续脚本。
Test it and let us know if it works for you.
测试它并告诉我们它是否适合您。
回答by ChrisH
In my opinion, it would be better to run cronjobs. They will execute synchronously. If the task is "on-the-fly", you could execute the command to add this cronjob. More information about cronjobs: http://unixgeeks.org/security/newbie/unix/cron-1.html
在我看来,运行 cronjobs 会更好。它们将同步执行。如果任务是“on-the-fly”,你可以执行命令来添加这个cronjob。有关 cronjobs 的更多信息:http://unixgeeks.org/security/newbie/unix/cron-1.html
http://service.futurequest.net/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=30
http://service.futurequest.net/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=30
回答by vinit agrawal
Both exec and system wait for the script to execute unless you don't fork.
除非你不 fork,否则 exec 和 system 都会等待脚本执行。
Check.php
检查.php
<?php
echo "here ".__LINE__."\n";
exec ("php phpscript1.php");
echo "here ".__LINE__."\n";
system("php phpscript2.php");
echo "here ".__LINE__."\n";
?>
phpscript1.php
phpscript1.php
<?php
echo "=================phpscript1.php\n";
sleep(5);
?>
phpscript2.php
phpscript2.php
<?php
echo "=================phpscript2.php\n";
sleep(5);
?>
Check.php execute script1 for 5 seconds then display next line number and then execute script2 by printing the next line.
Check.php 执行 script1 5 秒,然后显示下一行编号,然后通过打印下一行来执行 script2。