Command not Found & Proper way to cwd #746
-
hello thanks for this tool i just started with it, i want know what is the best way to start in a specific dir like other tools "cwd", now im doing like this in a helper function "run_cmd": run(f"cd {cwd}; {cmd}")
run(f"cd {cwd}; bash -c '{cmd}'") # or this also one command "yarn" which is installed using nvm -> npm . i tried doing the following but same issue (maybe other profile need to be sourced?) but i dont know the issue since when i login with ssh via terminal it work: cmd = await run_cmd("source ~/.bashrc; source ~/.profile; yarn") which yarn
/home/ubuntu/.nvm/versions/node/v20.19.0/bin/yarn |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You're on the right track here -- AsyncSSH and other implementations like OpenSSH don't really know or care at all about current working directory on a remote system, at least not when starting a shell or exec session. About the only time the working directory impacts SSH is when running SFTP. If you're running commands, it all comes down to telling the remote shell that's launched to change directory and then execute whatever command you want to run. If things get more complicated, you can always build all of this into a shell script that you place on the remote system and have your SSH command just execute that shell script. That lets you do things like conditionals or other multi-line gestures more easily. As for what startup files get run when starting a remote shell, that tends to vary based on the shell you are using and whether you ask SSH to launch an interactive shell or execute a command. Since you're doing the latter here and you mention bash, my experience is that bash will automatically source your ~/.bashrc in that case, but only source ~/.bash_profile (and NOT .bashrc) when starting an interactive shell. This is why you'll sometime see ~/.bash_profile end with sourcing ~/.bashrc itself. If you need something from a startup file other than ~/.bashrc, you will probably need to source it yourself before running the command (like you show above). |
Beta Was this translation helpful? Give feedback.
-
thansk for your answer glad to know that im on right track, about the command not found issue for packages of nvm npm the following solve it for now since sourcing bashrc or profile did not work> thanks again # this allow you to run any npm cli package
cmd = await run_cmd("source ~/.nvm/nvm.sh; yarn --version") # 1.22.22 |
Beta Was this translation helpful? Give feedback.
-
Glad you figured out the issue... Does your ~/.bash_profile happen to contain a "source ~/.nvm/nvm.sh" command in it? If so, that would explain things. When running commands over SSH, ~/.bash_profile won't be automatically sourced, and so that'll also fail to source anything it runs or set any environment variables it sets. However, if you were to SSH to the machine without specifying a command, that would drop you into an interactive bash where ~/.bash_profile WAS sourced. |
Beta Was this translation helpful? Give feedback.
You're on the right track here -- AsyncSSH and other implementations like OpenSSH don't really know or care at all about current working directory on a remote system, at least not when starting a shell or exec session. About the only time the working directory impacts SSH is when running SFTP. If you're running commands, it all comes down to telling the remote shell that's launched to change directory and then execute whatever command you want to run. If things get more complicated, you can always build all of this into a shell script that you place on the remote system and have your SSH command just execute that shell script. That lets you do things like conditionals or other multi-line …