how to run cronjob from python virtual environment

8 posts / 0 new
Last post
#1 Tue, 10/15/2019 - 10:47
pratam02

how to run cronjob from python virtual environment

I have a cronjob which needs to use python environment to run on schedule....

I tried first solution mentioned in in this tread, it is working when I click "Run Now" button in cron job set-up window but not working when i schedule it..... https://stackoverflow.com/questions/3287038/cron-and-virtualenv

Also I am not sure where it logs the error., so I can look for solution....

my jobs looks like this.... /home/user/myenv/bin/python /home/user/schedules/some.py

it perfectly runs with "Run Now" button with all users(ex: root, webiste-user)

Regards

Tue, 10/15/2019 - 15:16
unborn
unborn's picture

@pratam02

hi, I think what you need is here:

https://www.virtualmin.com/comment/793224#comment-793224

Configuring/troubleshooting Debian servers is always great fun

Wed, 10/16/2019 - 15:16
pratam02

Hi,

I followed the same steps before posting my question here....

1) I used full path of python environment. like this: /home/user/myenv/bin/python /home/user/schedules/some.py

2) I set domain owner to run this job.

not sure what's happening...

is there any place are we logging error for cron jobs.??

Thu, 10/17/2019 - 16:27
unborn
unborn's picture

@pratam02

sure, you can redirect output of that file temporary to log file or text file and that should tell you. I think you did not understand... the correct path should be /usr/bin/python /path/to/your/script.py and not /home/user/myenv/bin/python..

try to run it like this: /usr/bin/python /home/user/schedules/some.py

Configuring/troubleshooting Debian servers is always great fun

Fri, 10/18/2019 - 07:49
pratam02

Hello Unborn,

I believe you have not read the title of this post... I want to run python script with specific virtual environment. not from default virtual environment...

I already have a corn job which are running without any problem when I give path like above.... but when I use specific python virtual environment.... its not working....

I have to use this virtual environment because all the required package installed inside this virtual environment....

also as mentioned earlier... it works perfectly in cron job design window with "Run Now" button... but when you schedule it, it fails silently ....

Sat, 10/19/2019 - 05:41 (Reply to #5)
Jfro

Can't help but is this the case also?

The accepted answer actually works fine until your python need to run a script which have to run another python binary from venv/bin directory.

IF SO Maybe search for such term then on the web. should be something with (activate) i hope / guess ,

I find activate for python env nice that it is possible but a .......... thing to remember for using it only sometimes. ( wen i forget to deactivate for example)

Don't know if other here know that part, virtualmin support maybe can help you but i guess they are really busy for the moment with some update things. So for if GPL license?

IF PRO license you can post here for support from virtualmin support is quicker. ;)

https://www.virtualmin.com/project/issues

Fri, 10/18/2019 - 19:46
unborn
unborn's picture

Ah, sorry perhaps i missed that.. You're rigjt. Sorry.

Configuring/troubleshooting Debian servers is always great fun

Tue, 11/05/2019 - 10:13
pratam02

Thanks Jfro,

that worked for me...

now I created one more python file, where cron job runs this file from local environment. but inside that python file I am activating environment python and executing its job.......

example code:

#!/usr/bin/python

activate_this = "/home/username/pythonenv/bin/activate_this.py"

execfile(activate_this, dict(__file__=activate_this))

from module import YourClass
Topic locked