mafortis
mafortis

Reputation: 7128

How to run multiple command with exec in php

I need to run 2 command after each other with exec but it won't run.

Code

$command1 = 'cd '.$destination.''; //open destination folder (e.g. public_html)
$command2 = 'git clone '.$repos->repository.''; //make clone

$sshConnection1 = exec($command1); // run first command(open folder)
$sshConnection = exec($command2); //run second command (make clone)

Before I create this question I read some of suggested topics like this one. to add "&" etc. but no luck.

Please tell me what should I do in order to run both commands successfully.

Upvotes: 1

Views: 222

Answers (2)

Karsten Koop
Karsten Koop

Reputation: 2524

You can simply put both commands into one exec() call by combining them with &&:

exec("cd $destination && git clone {$repos->repository}");

Upvotes: 1

Nick Chambers
Nick Chambers

Reputation: 23

It looks like PHP's exec forks at some point, so that cd isn't going to affect the parent process.

php > echo getcwd();
/home/nchambers
php > exec("cd ..");
php > echo getcwd();
/home/nchambers
php > echo exec("pwd");
/home/nchambers
php > exec("cd ..");
php > echo exec("pwd");
/home/nchambers
php >

Also, git clone can take a destination directory to write to. I can't say why your commands are failing without more information, but just some initial problems with what you're trying to run.

Upvotes: 1

Related Questions