Marco Fumagalli
Marco Fumagalli

Reputation: 2467

virtualenv not activated from crontab on Linux Centos

I'm facing a weird issue.

I need to write a crontab which will invoke a python script, but I need to activate a virtualenv first. This is the crontab I wrote:

SHELL = /bin/bash
MAILTO="[email protected]"
*/15 * * * * source /srv/python/virtualenvs/proj/bin/activate && /srv/python/virtualenvs/proj/bin/python3.6 /srv/python/proj/Scripts/scheduling.py

The script scheduling.py try to import data from an oracle DB using Cx_Oracle. Crontab gives me an error:

[2018-12-05 14:45:02] ERROR - DB connection error: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html

So I obviously thought about an error related to Oracle and library cx_Oracle. The weird thing is that if I enter in the linux shell and do

source /srv/python/virtualenvs/proj/bin/activate

Then type python to open a python shell then:

import cx_Oracle
import pandas
con = cx_Oracle.connect('parameter_connection')
query = 'select * from tab1 fetch first 5 rows only'
pd.read_sql(query, con = con)

it works and gives me query result. I suspect that in crontab the virtualenv is not activated properly.

Any ideas? Thanks

Upvotes: 0

Views: 129

Answers (1)

Sk.
Sk.

Reputation: 460

in crontab

*/15 * * * * /home/user/script.sh > /dev/null 2>&1

in script.sh

#!/bin/bash
source /srv/python/virtualenvs/proj/bin/activate
/srv/python/virtualenvs/proj/bin/python3.6 /srv/python/proj/Scripts/scheduling.py

Upvotes: 2

Related Questions