Jump to content

PHP Exec Function Doesn't work with cd Ubuntu Command


unemployment

Recommended Posts

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?

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; 

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

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.');

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.');
  On 4/12/2013 at 2:29 AM, Christian F. said:

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.

deploy is the directory where my deploy script is located. 

Deploy script: /var/www/deploy/deploy.php

Trekeffect Main site: /var/www/trekeffect.com/trekeffect.com

The deploy script is supposed to change to that directory to run the git commands.

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.