Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I change the terminal working directory ? #445

Closed
arnolem opened this issue Apr 29, 2024 · 5 comments
Closed

How can I change the terminal working directory ? #445

arnolem opened this issue Apr 29, 2024 · 5 comments

Comments

@arnolem
Copy link

arnolem commented Apr 29, 2024

For instance:

/home/user/$ castor goto:projet-a
Success
/var/www/projects/projet-a

I tried :

run('cd /var/www/projects/projet-a');
run('', workingDirectory: '/var/www/projects/projet-a');
run('goto-projet-a.sh');

but nothing works 馃檮

@TheoD02
Copy link
Contributor

TheoD02 commented Apr 29, 2024

Hi there!

I'm not sure if I understood the request completely but from what I understand you want to execute an command in a specific folder?

For example the following command will output the content of /home/user/my-directory

run('ls', workingDirectory: '/home/user/my-directory');


But in the second case, and you want to change the directory of the terminal (not castor working directory)

Under the hood run use Symfony Process component and AFAIK is not possible to change de current working directory of terminal.

Not tested, but maybe chdir can do the job but care about the note

As alternative you can do something like this to run bash terminal in specific directory :

run('env --chdir=/home/theod/Downloads/ bash', tty: true, allowFailure: true);

But keep in mind that will run under castor and not the terminal directly like a simple cd and possibly add some strange behavior (not sure about that)

@arnolem
Copy link
Author

arnolem commented Apr 30, 2024

Thanks a lot.
I tried with chdir but that doesn't work either.

Your solution works but indeed, it would be good to know if there is an impact, particularly if we use the script several times, we will find ourselves launching several nested processes?

@arnolem
Copy link
Author

arnolem commented Apr 30, 2024

Unfortunately this workaround will not be satisfactory.

The processes add up and we must exit each bash to exit permanently

image

image

nice try anyway 馃憤

@tigitz
Copy link
Contributor

tigitz commented May 1, 2024

/home/user/$ castor goto:projet-a
Success
/var/www/projects/projet-a

Are you trying to make a castor script that would be the equivalent of running cd in your bash session?

Because to execute a PHP script from a Bash terminal that makes your Bash session switch to a specific directory, you'll need to handle the directory switch on the Bash side because a PHP script cannot directly manipulate the environment of the shell that calls it.

In any other cases, what Theo suggested is the way to go when something you run needs to be ran in a specific directory.

Maybe posting the full context of what you're trying to achieve could help.

@arnolem
Copy link
Author

arnolem commented May 2, 2024

Are you trying to make a castor script that would be the equivalent of running cd in your bash session?

yes, this is the feature I need to do 馃槕

@arnolem arnolem closed this as completed May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants