在 crontab 中,我添加了一个 php 脚本名称,从 CLI SAPI 模式运行这个脚本,所以没有 max_execution_time 问题。
我可以使用空格传递多个参数。
system('/path/of/your/script.php param1 param2 > scriptlog.txt &')
但我需要传递一个数组,因为参数是 shell 脚本并分解数组。
例如,
system('/path/of/your/script.php array > scriptlog.txt &')
最佳答案
当你施放 system
在你的应用程序中,你必须 implode
你的参数
只需像这样连续传递参数
system('/path/of/your/script.php param[0] param[1] > scriptlog.txt &')
这看起来像
system('/path/of/your/script.php '.implode(" ",$params).' > scriptlog.txt &')
如果你有报价,你可以看看 escapeshellarg
system('/path/of/your/script.php '.implode(" ",array_map("escapeshellarg",$params)).' > scriptlog.txt &')
然后在你的script.php
中,用
$args = $argv;
array_shift($args); //Because $args[0] is 'script.php'
如果您在 script.php
中捕获 > scriptlog.txt &
,请改用它:
$args = $argv;
if (false !== ($pos = array_search(">",$args))) {
$args = array_slice($args,1,$pos-1);
} else {
array_shift($args);
}
请注意,这仅在您的数组是非关联数组时才有效。
您需要编写另一个函数来检索关联参数
关于php - 在多线程之前在长时间运行的后台 shell 脚本中传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12095360/