S
S
shell_guy2021-11-17 15:04:06
Python
shell_guy, 2021-11-17 15:04:06

How to run cron tasks from python virtual env?

Hello, there are a lot of examples on the net and, as usual, they are not working.
The cron task does not work
. What I did:
1. Determined the path to the virtual environment echo $VIRTUAL_ENV
/opt/Python-3.8.7/proj
2. Added to crontab to run once a minute
* /1 * * * * /opt/Python-3.8.7/proj [path where the script is in fact->]/opt/run_base.py

Nothing happens. Why?
Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vindicar, 2021-11-17
@shell_guy

shell_guy , it is better to specify full paths to all files.
This is how it works for me, anyway.

cd /opt/my_python_project && sudo -u username /opt/my_python_project/bin/python3 /opt/my_python_project/main.py

where /opt/my_python_project is the path where you deployed venv
/opt/my_python_project/main.py is the executable project file
username is the username under which you want to run the project. If running under a specific account is not required, sudo can be omitted, but I would not run my script as root without a real need. You never know.

M
MrSpirit, 2021-11-17
@MrSpirit

I did this: cd python/project && source venv/bin/activate && python3 main.py

V
Vladimir Kuts, 2021-11-17
@fox_12

Well, probably because, firstly, that the path needs to be continued - something like:
/opt/Python-3.8.7/proj/bin/python
to the binary that your script will run
Secondly, take into account that your environment and the crontab environment are different ..
You may need to register some paths and so on .. .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question