Jump to content

PHP Exec Function Doesn't work with cd Ubuntu Command


unemployment

Recommended Posts

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.com

2013-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?

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

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.

  On 4/12/2013 at 9:46 PM, trq said:

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.

  • 10 months later...

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.