vc2310
vc2310

Reputation: 155

How to close and reopen shell with a bash script

So I am trying to install conda on a linux server. For this, I am running a bash script as a root user and I have made a new user which is going to install conda. The new user is "ags". Added below are lines from my shell script.

echo "Getting the conda installer"
su - ags -c "wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /${install_directory}/ags/miniconda.sh"
echo "Installing conda"
su - ags -c "bash /${install_directory}/ags/miniconda.sh -b -p /${install_directory}/ags/miniconda"


###ERROR PART UNDERNEATH ####
su - ags -c "/${install_directory}/ags/miniconda/condabin/conda init bash"

su - ags -c "export ARCGISHOME=/${install_directory}/ags/arcgis/server; conda activate

However, my output is asking me to restart shell:

root@my_server:~# su - ags -c "/data/ags/miniconda/condabin/conda init bash"
no change     //data/ags/miniconda/condabin/conda
no change     //data/ags/miniconda/bin/conda
no change     //data/ags/miniconda/bin/conda-env
no change     //data/ags/miniconda/bin/activate
no change     //data/ags/miniconda/bin/deactivate
no change     //data/ags/miniconda/etc/profile.d/conda.sh
no change     //data/ags/miniconda/etc/fish/conf.d/conda.fish
no change     //data/ags/miniconda/shell/condabin/Conda.psm1
no change     //data/ags/miniconda/shell/condabin/conda-hook.ps1
no change     //data/ags/miniconda/lib/python3.7/site-packages/xontrib/conda.xsh
no change     //data/ags/miniconda/etc/profile.d/conda.csh
modified      //data/ags/.bashrc

==> For changes to take effect, close and re-open your current shell. <==

root@my_server:~# su - ags -c "conda activate"
-sh: 1: conda: not found

Is there a way to restart my shell (ags) and still keep the script running after that?

Upvotes: 6

Views: 21002

Answers (1)

merv
merv

Reputation: 76810

Conda defines conda activate as a shell function and conda init puts code in init files (here .bashrc) to ensure the function gets defined at the start of interaction shell sessions. An alternative to restarting the session is to instead source ~/.bashrc.

It might be worth noting that a (usually minor) concern with manually re-sourcing an init file in an already active section is that some of the code in .bashrc could be non-idempotent (i.e., running it multiple times has a different effect than running it just once). Fatih Arslan has a nice blog post with tips about writing bash scripts that are idempotent by design.

Upvotes: 5

Related Questions