Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

is it possible to run multiple linux command using PHP. I am using Mongodb database and if I want to import multiple collections, I am running the following command for each collection individually.

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

Now I have at least 10 collections and I have to run each of them individually in linux command line. There should be a better way to do this. What I am thinking is to write a php script which will do this for me.

Any idea, suggestions will be really helpful. Thanks in advance.

share|improve this question

2 Answers

up vote 1 down vote accepted

You could have PHP create all of the shell commands beforehand and then run them all at once:

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

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

shell_exec($command);

This eliminates multiple calls to shell_exec(). However, perhaps mongoimport is available in the PHP mongo API.

share|improve this answer

You can run OS command-line commands from a PHP script using shell-exec. See http://php.net/manual/en/function.shell-exec.php.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.