unemployment Posted April 12, 2013 Share Posted April 12, 2013 Do you know why my exec() to change directory doesn't work when the script executes? $test = exec('cd /var/www/mysite.com/mysite.com', $output); echo $test; $ls = exec('ls'); echo $ls; Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/ Share on other sites More sharing options...
Christian F. Posted April 12, 2013 Share Posted April 12, 2013 (edited) Yes, "cd" does not change the working dir of PHP, but the "instance" of the shell that gets invoked using "exec". And each execution is a separate instance. Also, any particular reason why you're trying to use shell commands instead of the built-in PHP commands like glob? Edited April 12, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424274 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 Try to run a shell_exec() php function instead of exec(), it should work.  Exemple: $test = shell_exec('cd /var/www/mysite.com/mysite.com', $output); echo $test; If you want to list another directories where apache does not have permissions: //create a copy of catalogDB to attach $cd = shell_exec("chmod 755 /home/user/Downloads -R; cd /home/user/Downloads; ls -la ."); echo $cd; Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424280 Share on other sites More sharing options...
trq Posted April 12, 2013 Share Posted April 12, 2013 See also chdir Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424284 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 chdir works well. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424288 Share on other sites More sharing options...
Sanjib Sinha Posted April 12, 2013 Share Posted April 12, 2013 In shared hosting, i always encounter lot of problems while tying to execute these directory functions. Probably there is permission issue. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424289 Share on other sites More sharing options...
trq Posted April 12, 2013 Share Posted April 12, 2013 In shared hosting, i always encounter lot of problems while tying to execute these directory functions. Probably there is permission issue. Who the hell uses shared hosting these days? Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424290 Share on other sites More sharing options...
Sanjib Sinha Posted April 12, 2013 Share Posted April 12, 2013 getting dedicated server in India is extremely costly sir... Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424292 Share on other sites More sharing options...
Jessica Posted April 12, 2013 Share Posted April 12, 2013 Too bad this internets thing isn't global eh? Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424294 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 chdir works well. Thanks  A shell_exec() should be work too!  Try, $test = shell_exec('cd /var/www/mysite.com/mysite.com;pwd'); echo $test; Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424295 Share on other sites More sharing options...
Sanjib Sinha Posted April 12, 2013 Share Posted April 12, 2013 its a locational challenge. hope one day internet things will be globally same for everyone... Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424296 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 (edited) Hey guys... I change my script around and for some reason it doesn't seem to be working any more. Do you know why? $this->log(getcwd()); //chdir('/mysite.com/mysite.com'); shell_exec('cd /var/www/mysite.com/mysite.com', $output); $this->log(getcwd()); The out put for that is:2013-04-12 12:21:57-04:00 --- INFO: /var/www/deploy2013-04-12 12:21:57-04:00 --- INFO: /var/www/deploy  I don't receive an noticeable errors. I also tried the chdir function but that kills my script and the second log won't occur. Any ideas? Edited April 12, 2013 by unemployment Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424383 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 I think you do not understand very proper how those functions work. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424388 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 I'm writing an auto deploy script to automatically pull down my latest git deployment from bitbucket. Ideally, using the shell_exec command should make the most sense since I want terminal to execute the command. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424389 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 Yes, that's correct you can do this.  Write a simple bash script which take exerything that you want to get and execute that script by shell_exec().  That's all. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424390 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 (edited) I've already done that using the below code, but it doesn't work:Â $this->log('Enter Execute'); //$this->log($this->_directory); // Make sure we're in the right directory //chdir($this->_directory); $this->log(getcwd()); //chdir('/mysite.com/mysite.com'); shell_exec('cd /var/www/mysite.com/mysite.com', $output); $this->log(getcwd()); $this->log('Changing working directory... '); // Update the local repository exec('git pull '.$this->_remote.' '.$this->_branch, $output); $this->log('Pulling in changes... '.implode(' ', $output)); // Secure the .git directory exec('chmod -R og-rx .git'); $this->log('Securing .git directory... '); $this->log('Deployment successful.'); Edited April 12, 2013 by unemployment Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424394 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 Ok, step by step...... this is a not real directory - "/var/www/mysite.com/mysite.com" Â What is the real one? Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424400 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 This is my script with the real directory: $this->log('Enter Execute'); //$this->log($this->_directory); // Make sure we're in the right directory //chdir($this->_directory); $this->log(getcwd()); //chdir('/trekeffect.com/trekeffect.com'); shell_exec('cd /var/www/trekeffect.com/trekeffect.com', $output); $this->log(getcwd()); $this->log('Changing working directory... '); // Update the local repository exec('git pull '.$this->_remote.' '.$this->_branch, $output); $this->log('Pulling in changes... '.implode(' ', $output)); // Secure the .git directory exec('chmod -R og-rx .git'); $this->log('Securing .git directory... '); if (is_callable($this->post_deploy)) { call_user_func($this->post_deploy, $this->_data); } $this->log('Deployment successful.'); Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424401 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 Give me the message back: <?php $dir = shell_exec('pwd'); echo $dir; Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424404 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 The message back is /var/www/deploy Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424410 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 Sorry for delay but I'm working. Â So, the "deploy" is your working directory on the server not "trekeffect.com" Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424412 Share on other sites More sharing options...
Christian F. Posted April 12, 2013 Share Posted April 12, 2013 (edited) Yes, "cd" does not change the working dir of PHP, but the "instance" of the shell that gets invoked using "exec". And each execution is a separate instance. Same goes for shell_exec, btw. Edited April 12, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424416 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 deploy is the directory where my deploy script is located. Deploy script: /var/www/deploy/deploy.phpTrekeffect Main site: /var/www/trekeffect.com/trekeffect.comThe deploy script is supposed to change to that directory to run the git commands. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424418 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 What will you get: <?php $dir = shell_exec('cd /var/www/trekeffect.com/trekeffect.com;pwd'); echo $dir; Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424420 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 That returned:/var/www/trekeffect.com/trekeffect.com Which seems correct, but for some reason my script still didn't take. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/#findComment-1424422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.