skip to Main Content

I am writing a bash shell script that I can run on my local computer to update and redeploy a simple app hosted on an AWS E2 instance. I am having trouble with one line in particular: I cannot seem to activate a conda environment within the ssh command.

ssh -i $private_key_file $public_dns "conda activate ENVIRONMENT_NAME"

Returns:
bash: line 1: conda: command not found

However, if I ssh in with ssh -i $private_key_file $public_dns and then run conda activate ENVIRONMENT_NAME separatly this works fine.

What is happening? All other commands in my shell script are working fine with the ssh -i $private_key_file $public_dns "COMMAND" syntax.

My instance is running Ubuntu 22.04 if relevant.

2

Answers


  1. Chosen as BEST ANSWER

    After some more searching, I found my .bashrc file contained the following lines:

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    I solved this problem by simply moving my conda block above this line. I found this from the "community wiki"-owned response to this question.

    What I tried:

    • Sourcing my .bashrc file prior to running conda activate ENVIRONMENT_NAME. This did not solve the issue, apparently because it was still leaving the script on my non-interactive session.
    • Sourcing .bashrc in a .bash_profile file, which is recommended as another top answer in the thread linked above. While this does work in sourcing .bashrc, it still exists at the non-interactive condition.

  2. ssh -i $private_key_file $public_dns "conda activate ENVIRONMENT_NAME"
    

    Returns: bash: line 1: conda: command not found

    Because the conda command is not found in the path.

    However, if I ssh in with ssh -i $private_key_file $public_dns and
    then run conda activate ENVIRONMENT_NAME separatly this works fine.

    Plausible.

    What is happening?

    Your conda command is not present in the system’s default path. Likely you have installed a personal copy rather than a system-wide copy. Your personal shell setup scripts, .bash_profile and / or .bashrc prepare your interactive shells to run the conda command, but these are not read when you launch a shell non-interactively, as ssh does on your behalf in the first example.

    You could do something like

    ssh -i $private_key_file $public_dns ". .bashrc && conda activate ENVIRONMENT_NAME"
    

    , but I don’t see the point, because the shell in which you activate conda terminates immediately afterward, mooting the whole exercise. Whatever it is you want to run in your conda environment, consider writing a wrapper script on the E2 side that takes care of running it in the right environment.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search