<div dir="ltr"><div>HI Sam,</div><div><br></div><div>I don't fully understand exactly the issue, as I do not have a testing environment to replicate what you are seeing. However, I suspect the core of the issue is that when the cron job is run by the system, it's not *your* .bashrc file that is used, rather one elsewhere on the system (I don't know offhand where, or if any, is used).</div><div><br></div><div>All that "conda init" does is append these lines to the .bashrc in your homedir (it appended to .bash_profile on my Mac):</div><div><br></div><div># >>> conda initialize >>><br># !! Contents within this block are managed by 'conda init' !!<br><span style="color:rgb(255,0,0)">__conda_setup="$('/Users/brownrig/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"</span><br>if [ $? -eq 0 ]; then<br>    <span style="color:rgb(255,0,0)">eval "$__conda_setup"</span><br>else<br>    if [ -f "/Users/brownrig/miniconda3/etc/profile.d/conda.sh" ]; then<br>        . "/Users/brownrig/miniconda3/etc/profile.d/conda.sh"<br>    else<br>        export PATH="/Users/brownrig/miniconda3/bin:$PATH"<br>    fi<br>fi<br>unset __conda_setup<br># <<< conda initialize <<<</div><div><br></div><div>Perhaps placing this into your bash script before the call to "conda activate". Minimally the lines in red are enough to set up the environment. There may be a better way to do this. Perhaps invoking bash from cron with "--rcfile /path/to/your/.bashrc"</div><div><br></div><div>Hope that helps...</div><div>Rick</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Dec 31, 2020 at 12:31 PM Sam McClatchie via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu">ncl-talk@mailman.ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  
    
  
  <div>
    <p><font face="TeX Gyre Adventor">Colleagues</font></p>
    <p><font face="TeX Gyre Adventor">[I am using Ubuntu 18.04]<br>
      </font></p>
    <p><font face="TeX Gyre Adventor">I want to call an NCL script from
        a bash shell script. Normally I would just activate the
        environment from the command line:</font></p>
    <p><font face="TeX Gyre Adventor">(base) smcc@Jaguar:~$ conda
        activate ncl_stable<br>
        (ncl_stable) smcc@Jaguar:~$ <br>
      </font></p>
    <p><font face="TeX Gyre Adventor">and then run the shell script in
        the terminal:</font></p>
    <p><font face="TeX Gyre Adventor">./download_latest_GHRSST_data_from_ERDdap.sh</font></p>
    <p><font face="TeX Gyre Adventor">This works fine.<br>
      </font></p>
    <p><font face="TeX Gyre Adventor">The problem is that I want to
        automate by using a shell script called in a cron job. </font><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">I want
          to incorporate this NCL script in a cron job containing other
          calls to R, so being able to activate the NCL environment from
          within a shell script would be helpful. I suspect others have
          done something similar, so I'd appreciate your guidance.</font></font></p>
    <p><font face="TeX Gyre Adventor">### details follow
        #########################<br>
      </font></p>
    <p><font face="TeX Gyre Adventor">When I try to activate the NCL
        environment from a bash shell script using:</font></p>
    <p><font face="TeX Gyre Adventor">/home/smcc/anaconda3/bin/conda
        activate ncl_stable</font></p>
    <p><font face="TeX Gyre Adventor">in the shell script, the following
        error is returned: <br>
      </font></p>
    <p><font face="TeX Gyre Adventor">(base)
smcc@Jaguar:/mnt/data/dynamic_data/projects/projects2020/recreational_fishing_GHRSST/shell$
        ./download_latest_GHRSST_data_from_ERDdap.sh <br>
        <br>
        Error message start ---------------------------</font></p>
    <p><font face="TeX Gyre Adventor">CommandNotFoundError: Your shell
        has not been properly configured to use 'conda activate'.<br>
        To initialize your shell, run<br>
        <br>
            $ conda init <SHELL_NAME><br>
        <br>
        Currently supported shells are:<br>
          - bash<br>
          - fish<br>
          - tcsh<br>
          - xonsh<br>
          - zsh<br>
          - powershell<br>
        <br>
        See 'conda init --help' for more information and options.<br>
        <br>
        IMPORTANT: You may need to close and restart your shell after
        running 'conda init'.<br>
        <br>
        ./download_latest_GHRSST_data_from_ERDdap.sh: line 19: ncl:
        command not found<br>
        (base)
smcc@Jaguar:/mnt/data/dynamic_data/projects/projects2020/recreational_fishing_GHRSST/shell$
        <br>
      </font><font face="TeX Gyre Adventor"><font face="TeX Gyre
          Adventor">Error message end </font>--------------------------------------------<br>
      </font></p>
    <p><font face="TeX Gyre Adventor">Running conda init bash before
        calling the shell script does not fix the problem. <br>
      </font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">(ncl_stable)
smcc@Jaguar:/mnt/data/dynamic_data/projects/projects2020/recreational_fishing_GHRSST/shell$
          conda init bash<br>
          no change     /home/smcc/anaconda3/condabin/conda<br>
          no change     /home/smcc/anaconda3/bin/conda<br>
          no change     /home/smcc/anaconda3/bin/conda-env<br>
          no change     /home/smcc/anaconda3/bin/activate<br>
          no change     /home/smcc/anaconda3/bin/deactivate<br>
          no change     /home/smcc/anaconda3/etc/profile.d/conda.sh<br>
          no change     /home/smcc/anaconda3/etc/fish/conf.d/conda.fish<br>
          no change     /home/smcc/anaconda3/shell/condabin/Conda.psm1<br>
          no change    
          /home/smcc/anaconda3/shell/condabin/conda-hook.ps1<br>
          no change    
          /home/smcc/anaconda3/lib/python3.7/site-packages/xontrib/conda.xsh<br>
          no change     /home/smcc/anaconda3/etc/profile.d/conda.csh<br>
          no change     /home/smcc/.bashrc<br>
          No action taken.</font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">Another
          recommendation was to prepend a line to the bash script:</font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">eval
          "$(command conda 'shell.bash' 'hook' 2> /dev/null)"</font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">but
          this doesn't seem to work when I call the shell script from
          cron. <br>
        </font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">My
          cron file is just one line calling the shell script to run
          interactively:</font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">0
          12 * * * bash -i
/mnt/data/dynamic_data/projects/projects2020/recreational_fishing_GHRSST/shell/download_latest_GHRSST_data_from_ERDdap.sh</font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">and
          here is my shell script:</font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">start
          shell script ------------------------------</font></font></p>
    <p><font face="TeX Gyre Adventor"><font face="TeX Gyre Adventor">eval
          "$(command conda 'shell.bash' 'hook' 2> /dev/null)"<br>
          #!/bin/bash<br>
          ## download_latest_GHRSST_data_from_ERDdap.sh<br>
          # NOTE: for this script to work, call the script in cron
          using: <br>
          # "bash -i ./download_latest_GHRSST_data_from_ERDdap.sh"<br>
          # to run the script in interactive mode so the .bashrc file is
          sourced<br>
          # Otherwise conda activate will not work<br>
          <br>
          # Use crontab -e to edit the cron file <br>
          # (see
          <a href="https://linux4one.com/how-to-set-cron-jobs-on-ubuntu-18-04/" target="_blank">https://linux4one.com/how-to-set-cron-jobs-on-ubuntu-18-04/</a>)<br>
          # use cronitor to troubleshoot cron file <br>
          # (see
          <a href="https://cronitor.io/cron-reference/cron-troubleshooting-guide" target="_blank">https://cronitor.io/cron-reference/cron-troubleshooting-guide</a>).
          <br>
          # Try $ cronitor select to test run<br>
          <br>
          ########################################<br>
          # download the latest GHRSST data from erddap<br>
          ########################################<br>
           # Rscript
"/mnt/data/dynamic_data/projects/projects2020/recreational_fishing_GHRSST/R/download_GHRSST_MUR-JPL-L4-GLOB-v4.1_daily.R"<br>
          <br>
          ########################################<br>
          # plot GHRSST data<br>
          ########################################<br>
          # activate the ncl environment<br>
          conda activate ncl_stable<br>
          <br>
          ncl
/mnt/data/dynamic_data/projects/projects2020/recreational_fishing_GHRSST/ncl/plot_daily_GHRSST-MUR-JPL-L4-GLOB-v4_1_around_New_Zealand.ncl<br>
          <br>
        </font></font><font face="TeX Gyre Adventor"><font face="TeX
          Gyre Adventor"><font face="TeX Gyre Adventor"><font face="TeX
              Gyre Adventor">end shell script </font></font>-------------------------------<br>
        </font></font></p>
    <p><font face="TeX Gyre Adventor">Best fishes</font></p>
    <p><font face="TeX Gyre Adventor">Sam<br>
      </font></p>
    <div>-- <br>
      
      
      Sam McClatchie (fisheries oceanographer)<br>
      & Elena Turin (accounting & auditing)<br>
      FishOcean Enterprises <span style="text-decoration:underline"><a href="http://www.fishocean.info" target="_blank">www.fishocean.info</a></span><br>
      38 Upland Rd, Huia, Auckland 0604, New Zealand<br>
      cell: 027 752 8495<br>
      
      <img style="width: 150px; height: 149px;" alt="" src="cid:176c9378d8eb74f45fb1"><br>
      <br>
      "The time has come", the tui said,<br>
      "to talk of many things:<br>
      Of songs - and ferns - and flowering flax,<br>
      of pukekos and dreams ..."<br>
      <br>
      (not Lewis Carroll) </div>
  </div>

_______________________________________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="https://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">https://mailman.ucar.edu/mailman/listinfo/ncl-talk</a></blockquote></div>