1

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.

mafortis
  • 6,750
  • 23
  • 130
  • 288

2 Answers2

1

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.

1

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

exec("cd $destination && git clone {$repos->repository}");
Karsten Koop
  • 2,475
  • 1
  • 18
  • 23