0

See my shell script:

#!/bin/bash
eval "$(conda shell.bash hook)"
conda activate myenvironment
cd /home/user/myproject/
python main.py

If I call it with bash being logged in as user, it works. But then I try calling it with this cronjob:

01 03 * * * /home/user/myproject/run.sh

It gives me this error:

/home/user/myproject/run.sh: line 2: conda: command not found
/home/user/myproject/run.sh: line 3: conda: command not found
/home/user/myproject/run.sh: line 5: python: command not found

Other relevant output from the cronjob:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/user>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=user>

How can I fix this?

Leo
  • 222
  • 1
  • 4
  • 15
  • Where are your `conda` and `python` binaries located ? Are they in `/usr/bin` or `/bin` ? – Aserre Jun 23 '21 at 09:28
  • so conda is in `/home/user/miniconda3/condabin/conda` – Leo Jun 23 '21 at 12:35
  • You have `X-Cron-Env: `. `/home/user/miniconda3/condabin/` is not in your PATH – Aserre Jun 23 '21 at 12:38
  • yes, this seems to be the solution! Will it be enough if I specify the path to the conda? when it then activates the conda, will conda have all the environment variables as I am used to? – Leo Jun 23 '21 at 12:43
  • 1
    If you use the full path in your script, it will work. Regarding the variables, it depends. How are those variables set up ? Your cron seems to be configured to run with your user, but it is using `/bin/sh`, not `bash`, so it wont use any configuration from your `.bash_profile`, `.bashrc`, etc ... – Aserre Jun 23 '21 at 12:46
  • this is actually a good hint. I will try this solution: https://unix.stackexchange.com/questions/94456/how-to-change-cron-shell-sh-to-bash – Leo Jun 23 '21 at 12:48
  • Similar: https://stackoverflow.com/questions/66337313/schedule-python-script-with-crontab/66355572#66355572 – merv Jun 23 '21 at 18:04

0 Answers0