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.

3
  • @Amessihel but they won't , I checked first command with dd($sshConnection1); and it returns false Commented Apr 14, 2019 at 18:53
  • 1
    How about exec("cd $destination && git clone ...") Commented Apr 14, 2019 at 19:31
  • @KarstenKoop thanks dude it works, please share it as answer, i'll so i can accept it Commented Apr 14, 2019 at 19:38

2 Answers 2

1

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

exec("cd $destination && git clone {$repos->repository}");
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.