php - 在多线程之前在长时间运行的后台 shell 脚本中传递数组

标签 php linux multithreading shell

在 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/

相关文章:

php - 数组 : 4th dimension, isset 返回不可靠

php - 使用 PHP 在数据库中搜索加密数据

javascript - 从 javascript 向 Yii Controller 发送数据

linux - 使用相同的配置和构建实现多个 jenkins

Python3-Pyqt5如何通过异常或按退出按钮结束线程中的循环

JavaScript 被注入(inject)我的 PHP 页面

linux - Makefile 规则未在 Linux 中执行

Python - 什么时候可以使用 os.system() 发出常见的 Linux 命令

c++ - 为什么在Release模式下的while循环中我无法逃脱?

Java:获得一次锁更好还是多次获得锁更好?