unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 (edited) When I run $dir = shell_exec('cd /var/www/trekeffect.com/trekeffect.com;pwd'); $this->log($dir); //chdir('/trekeffect.com/trekeffect.com'); //shell_exec('cd /var/www/trekeffect.com/trekeffect.com', $output); $test = shell_exec('pwd'); $this->log($test); It logs:2013-04-12 14:36:36-04:00 --- INFO: /var/www/trekeffect.com/trekeffect.com2013-04-12 14:36:36-04:00 --- INFO: /var/www/deploy It seems to be going back to the first deploy directory. Do you know why? 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/page/2/#findComment-1424424 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 (edited) Wow.....very strange directories names You don't have to execute shell_exec() every time, just put your logic into a simple string and execute it at once. Something like: <?php $str = 'cd /var/www/trekeffect.com/trekeffect.com;'; $str .='pwd;'; $str .= 'ls -d'; // etc..... //execute the string $dir = shell_exec($str); echo $dir; Don't forget to separate the individual unix commands with semicolon Edited April 12, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/page/2/#findComment-1424431 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 Ahh ok. I'll try that! Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/page/2/#findComment-1424432 Share on other sites More sharing options...
unemployment Posted April 12, 2013 Author Share Posted April 12, 2013 That works. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/page/2/#findComment-1424442 Share on other sites More sharing options...
trq Posted April 12, 2013 Share Posted April 12, 2013 shell_exec executes code in a new shell process. Meaning, if you call cd, it only changes directory for that process. Any subsequent calls to shell_exec will be executed from the original directory. I don't see why you would be using php for this at all. Use bash, and your issues will go away. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/page/2/#findComment-1424465 Share on other sites More sharing options...
jazzman1 Posted April 12, 2013 Share Posted April 12, 2013 shell_exec executes code in a new shell process. Meaning, if you call cd, it only changes directory for that process. Any subsequent calls to shell_exec will be executed from the original directory. I don't see why you would be using php for this at all. Use bash, and your issues will go away. Tony, I'm agree with you, in that case BASH is gonna be much more suitable and powerful than php. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/page/2/#findComment-1424486 Share on other sites More sharing options...
Mudassar Posted February 25, 2014 Share Posted February 25, 2014 I am reading Git log in php file at runtime. I have implement same in Linux and its working fine but I am not able to do same in window machine. Is there any way do this. I have install Git bash on window. So any help will be much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/page/2/#findComment-1470588 Share on other sites More sharing options...
jazzman1 Posted February 25, 2014 Share Posted February 25, 2014 Why don't you open a new thread instead extend this one? Nobody can charge you to create a new topic. Quote Link to comment https://forums.phpfreaks.com/topic/276848-php-exec-function-doesnt-work-with-cd-ubuntu-command/page/2/#findComment-1470590 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.