4

If I log in to my remote Mac via ssh -p22 jenkins@192.168.2.220 and type docker, it finds the executable because it also finds the path /usr/local/bin if I check with echo $PATH. But if I do the same in a heredoc inside a file setup-mac.sh like

#!/bin/bash
ssh jenkins@192.168.2.220 '/bin/bash -s' << 'EOF'
"echo $PATH" 
"bash run-docker.sh"
EOF

which I execute via shell and bash setup-mac.sh it does not find /usr/local/bin in PATH and consequently does not run docker, because the command is unknown.

On the remote Mac, there is a file run-docker.sh which is a bash file that calls docker commands, and it works if called locally.

To solve this issue, I've enabled PermitUserEnvironment on the mac in sshd_config, but this did not work. Though I only restarted ssh service and not the whole machine. Meanwhile I've changed all docker commands on the remote run-docker.sh script to an alias ${DOCKER} and I initialize it at the beginning of the script to DOCKER=/usr/local/bin/docker, but this is only a workaround.

user2366975
  • 4,350
  • 9
  • 47
  • 87
  • What’s the full path for the run-docker.sh file? – Bogdan Stoica Nov 28 '18 at 06:01
  • the `docker-run.sh` file is in `/Users/jenkins/Desktop/winlin/ubuntu`. – user2366975 Nov 28 '18 at 15:46
  • Please include complete error messages. See [Why should I post complete errors? Why isn't the message itself enough?](https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough) – that other guy Nov 28 '18 at 21:27

1 Answers1

4

I guess that the problem is occurring because /usr/local/bin is being added to the PATH by the 'jenkins' user's personal initialization file (~/.bashrc). That is run only by interactive shells, and the shell run by ssh ... '/bin/bash -s' ... is not interactive.

You could try forcing the shell to be interactive by invoking it with /bin/bash -i -s, but that is likely to cause other problems. (The shell may try and fail to set up job control. The value of PS1 may appear in outputs. ...)

In general, you can't rely on the PATH being set correctly for programs. See Setting the PATH in Scripts - Scripting OS X for a thorough analysis of the problem. Correct way to use Linux commands in bash script is also relevant, but doesn't have much information.

A simple and reliable way to fix the problem permanently is to set the required PATH explicitly at the start of run-docker.sh. For example:

export PATH=/bin:/usr/bin:/usr/local/bin

You may need to add other directories to the path if run-docker.sh runs programs that are in other locations.

Another solution to the problem is to use full paths to commands in code. However, that makes the code more difficult to read, more difficult to maintain, and more difficult to test. Setting a safe PATH is usually a better option.

pjh
  • 6,388
  • 2
  • 16
  • 17