如何使用PHP运行多个linux命令

时间:2013-11-09 20:40:09

标签: php ruby-on-rails linux mongodb mongodb-php

是否可以使用PHP运行多个linux命令。我正在使用Mongodb数据库,如果我想导入多个集合,我将为每个集合单独运行以下命令。

mongoimport --db test --collection colour --file colour.json
mongoimport --db test --collection shape --file shape.json
mongoimport --db test --collection size --file size.json

现在我至少有10个集合,我必须在linux命令行中单独运行它们。应该有更好的方法来做到这一点。我在想的是写一个PHP脚本,它会为我做这个。

任何想法,建议都会非常有用。提前致谢。

2 个答案:

答案 0 :(得分:2)

您可以让PHP预先创建所有shell命令,然后一次运行它们:

$collections = array('color', 'shape', 'size');
$command = '';

foreach($collections as $collection) {
    $command .= 'mongoimport --db test --collection ' . $collection . ' --file ' . $collection . '.json; ';
}

shell_exec($command);

这消除了对shell_exec()的多次调用。但是,PHP mongo API可能会mongoimport

答案 1 :(得分:0)

您可以使用shell-exec从PHP脚本运行OS命令行命令。请参阅http://php.net/manual/en/function.shell-exec.php