简体   繁体   中英

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.

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.

您可以通过将它们与&&组合在一起,将两个命令简单地放入一个exec()调用中:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM