简体   繁体   中英

Activating conda environment in bash script that runs on startup

So I have a python script that generates an animation - and it requires libraries that I have in a conda environment. I need to run this script as soon as my computer turns on, so I've written a short bash script that I added to "startup applications". This bash script runs on startup, and reads like this:

#!/bin/bash

conda activate myenv
cd ~/scripts
python generate.py

When I run this in terminal myself, it's fine, but whenever I turn on the computer, the python part of the script doesn't execute, and when I check errors i find:

conda: command not found

and then i also see the python script failed to run because it's missing libraries (from the conda environment not activating)

I have tried adding lines to the bash script replacing "conda activate" with "source activate", I have tried adding echo ". /home/<user>/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc to the bash script, replacing "conda" with /home/barrat/anaconda3/bin/conda , and even adding whoami to the bash script that runs at startup to make sure that i haven't magically become root by chance... none of this has worked. I would really appreciate any help. it's 3 AM and i'm a bit desperate.

You might have solved the issue yet, but for future viewers, this worked for me:

 if [ -f "/path/to/anaconda3/etc/profile.d/conda.sh" ]; then . "/path/to/anaconda3/etc/profile.d/conda.sh" CONDA_CHANGEPS1=false conda activate myenv fi 

Add this instead of conda activate myenv .

Well as you are trying to activate an environment to start your scripts, it may also be possible for you to make a startup script itself to do the desired task by using subprocess module from python.

Try making a demo.py script like :

import os
import system
import subprocess
import x

subprocess.run(["command name", "value"]) #for all scripts you want to execute

and then you can put this python script to run at startup. You can start quite a few amount of operations without noticable speed changes to your system and always can monitor it easily by starting the processes one after the other using time.sleep() in between two calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM