-1

When I run

gcloud compute ssh user@instance-1

And then when I am ssh'd in, I type pm2 and it prints out a help message, as expected.

If I instead run

gcloud compute ssh user@instance-1 --command "pm2"

It prints out bash: pm2: command not found

I had previously installed pm2 (by running npm install -g pm2 as both the root user and user for good measure).

What is going on here?

Infamous911
  • 1,441
  • 2
  • 26
  • 41

1 Answers1

2

My assumption is that in the first recipe, you perform a login meaning that your .profile and/or .bashrc scripts are run which modify your PATH environment variable to include the location of the command you wish to run (pm2). In the second recipe, where you supply --command my assumption is that the executable is being forked/execed (run directly) without your profile scripts being executed. This results in the command not found. Maybe try and run a script (through command) that logs your environment variables (for example /bin/env) and see how they differ from what you find when you are actually logged in.

Another solution would be to supply the full path to pm2 in your command.

Kolban
  • 13,794
  • 3
  • 38
  • 60
  • Yeah this is definitely it. When I echo $PATH through the ssh --command it prints my local (not remote) path. I can't get the remote to use its own PATH through editing .bashrc or .profile or anything else. Would you know how I can make it so that the remote uses its own PATH as set in .bashrc or elsewhere so that I don't have to manually set the path everytime i want to send one ssh command through the command line? – Infamous911 Jun 11 '19 at 03:26
  • 1
    I think we need to understand the framework at play. You can always run a command by specifying its full path for execution ... that would be one story. Next, we need to understand that the PATH environment variable is set in /etc/environment as a base level. If you need, you can augment that to include additional directories ... which would mean that they are present for all. Instead of considering "how to run .bashrc or .profile", think bigger and understand what running those commands does to PATH. – Kolban Jun 11 '19 at 03:47
  • 1
    I found my answer here https://stackoverflow.com/questions/940533/how-do-i-set-path-such-that-ssh-userhost-command-works -- thanks for the guidance! – Infamous911 Jun 12 '19 at 00:06