That my python script:
connection = cx_Oracle.connect(
'REPOR',
'REPOR',
'192.168.10.50:1521/db',
encoding='UTF-8'
)
cursor = connection.cursor()
try:
command = "df -h / /u01"
result = subprocess.check_output(command,
shell=True).decode(sys.stdout.encoding)
row = [('dm01dbadm01', result, datetime.now(), '0')]
cursor.bindarraysize = 1
cursor.executemany("insert into HISTORY values (:1, :2, :3, :4)", row)
connection.commit()
cursor.close()
print("Ok!")
except:
print("Oooops! Error!"
this script writes the result of this df -h command to the database.
Runs perfectly fine in linux terminal
From terminal i m excecuting the script with following commands:
export ORACLE_HOME=/u01/app/oracle/product/12.1.0.2/dbhome_1 scl enable python27 -- python2.7 /u01/app/Scripts/script.py
To run from crontab i did the following:
Created bash script with following entries:
export ORACLE_HOME=/u01/app/oracle/product/12.1.0.2/dbhome_1 scl enable python27 -- python2.7 /u01/app/Scripts/script.py
After cron is excecuting the script, but it shows Oooops! Error!. So, why the script is not excecuting from crontab as well?
Shouldn't you import the crx_Oracle and subprocess modules first?
Also refer to the "scl" tool by its path.
A crontab script does not get the full initialization that a login shell gets. Check the crontab documentation to get an exact description of the runtime environment that your script must run within.