1

Trying to send some output to Slack using cron on an instance of GCP Compute Engine running Linux Ubuntu 18.04.2 LTS.

Output is generated by python script.

Python script is usually run using conda activate my_env and python my_script.py

I have made the bash file executable by doing chmod +x my_script.bash

Here is content of bash file:

#!/bin/bash
source /home/user/miniconda3/bin/activate
conda activate my_env
python /home/user/folder/cron/reports.py -r check_stocks

I would expect adding the following line to crontab -e:

00 21 * * * cd /home/user/folder/cron/ && /bin/bash my_script.bash would give me the same results.

I run cd /home/user/folder/cron/ && /bin/bash my_script.bash in my shell and the script runs fine.

yongsheng
  • 376
  • 3
  • 19

2 Answers2

1

Make your .py file exacutable too (chmod +x file.py) - otherwise it won't work. You can find similar issue resolved here.

Wojtek_B
  • 4,245
  • 1
  • 7
  • 21
0

I had a similar issue and I gave up activating the conda environment and instead called directly the python bin in the miniconda environment folder, like this:

00 21 * * * /home/myuser/miniconda3/envs/my_env/bin/python /home/user/folder/cron/reports.py

I don't think this is the recommended solution, especially if you have a complex project with dependencies and you are importing a lot of modules, but for my simple script it solved the problem.

Giacomo
  • 1,796
  • 1
  • 24
  • 35