Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Python] - Python can't be executed by cron

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
guilherme.stefani
Posts: 1
Joined: 2022-12-21 17:05

[Python] - Python can't be executed by cron

#1 Post by guilherme.stefani »

Hi guys!

first of all I already search around about 2 day and can not find a useful solution, so I hope ask in forum would fix it

I need to run an Python script every 2 minuts... so I tried to configure it on a Cron.
# crontab -e

*/2 * * * * /usr/local/bin/python3.8 /dados/python_script/1.Rotinas_Automatizadas/Alertas/BI.py
If I check de cron log, everything apperas to be fine
# grep CRON /var/log/syslo

Dec 21 00:15:01 srv-debian CRON[5263]: (root) CMD (/usr/local/bin/python3.8 /dados/python_script/1.Rotinas_Automatizadas/Alertas/BI.py)
Dec 21 00:15:02 srv-debian CRON[5262]: (CRON) info (No MTA installed, discarding output)
If I execute manually this command , works fine too.
/usr/local/bin/python3.8 /dados/python_script/1.Rotinas_Automatizadas/Alertas/BI.py


My python Script:
#!/usr/bin/env python3.8

from sendmail import *
import cx_Oracle

#LOCALIZAÇÃO DOS ARQUIVOS QUE O CX_ORACLE UTILIZA PARA CONEXÃO
lib_dir = r"/dados/instantclient_11_2/"
cx_Oracle.init_oracle_client(lib_dir)


def conection(user, password, dsn):
connect_String = cx_Oracle.connect(user = user, password = password,dsn = dsn)
return connect_String

def insertError():
string = ""
sql = """SELECT v.ID, v.BLOCO, to_char(v.DT_HR_INICIO, 'DD/MM/YYYY HH24:MI:SS'), to_char(v.DT_HR_FIM, 'DD/MM/YYYY HH24:MI:SS'),
v.PARAMETRO, v.STATUS, v.OBS
FROM VM_DETALHES v where id not in(select id from aux_log)
and v.STATUS NOT IN ('ATUALIZANDO','FINALIZADO')"""
cursorF.execute(sql)
dataError = cursorF.fetchall()
if len(dataError) != 0:
for i in range(0,len(dataError)):
cursorF.execute(f"INSERT INTO AUX_LOG VALUES ({dataError[0]}, sysdate)")
string += "<p> ID: "+ str(dataError[0])+ "</p>" + "<p> PROCEDURE: "+str(dataError[1]) + "<p/> <p>" + "DATA/HORA: "+str(dataError[3]) + "</p> <p>" + "ERRO: " +str(dataError[6])+"</p> ====================================================="

lucas = Mail("EMAIL@DOMINIO.com.br","Log de atualização do BI", string)
lucas.send_mail()
guilherme = Mail("EMAIL2@DOMINIO.com.br","Log de atualização do BI", string)
guilherme.send_mail()
fumacense.commit()

#fumacense = conection("dwu","**********","DOMAIN.EXAMPLE.com.br:1521/rice")
fumacense = conection("dwu","**********","192.168.20.21:1521/kiarroz")

cursorF = fumacense.cursor()
insertError()


Thanks in advance for your help!!

Informations:
Python 3.8
SO Debian 10 (Buster)

peter_irich
Posts: 1403
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: [Python] - Python can't be executed by cron

#2 Post by peter_irich »

Check that the all utilits are called from script with full path.
Far example, /usr/bin/import.

peer
Posts: 441
Joined: 2017-03-26 10:14
Has thanked: 7 times
Been thanked: 19 times

Re: [Python] - Python can't be executed by cron

#3 Post by peer »

I also had a problem with starting a python script, in my case it was at startup. I discovered that the workdirectory of my script was my home directory, not the path of the python script
My solution was to set the workdirectory as first command in the script. In your case this would be:
os.chdir("/usr/local/bin/python3.8 /dados/python_script")
Perhaps this works for you also.

reinob
Posts: 1189
Joined: 2014-06-30 11:42
Has thanked: 97 times
Been thanked: 45 times

Re: [Python] - Python can't be executed by cron

#4 Post by reinob »

I can't seem to be able to recognize what your problem is. From your post everything looks OK, so maybe you could explain what the actual problem is?

(Hint: maybe it's something related to running the python script as root, but I'm only guessing).

schard
Posts: 1
Joined: 2023-01-06 23:00
Been thanked: 1 time

Re: [Python] - Python can't be executed by cron

#5 Post by schard »

There are several issues:
  • There is a trailing space in that path name at folder "python3.8 ". This should not be there.
  • Your python script is full of syntax errors due to missing indentation. It may be a copy-paste error, but we can only work with what you provide us with here. Your script, as you provided it to us, is utterly broken.

User avatar
donald
Debian Developer, Site Admin
Debian Developer, Site Admin
Posts: 1041
Joined: 2021-03-30 20:08
Has thanked: 185 times
Been thanked: 240 times

Re: [Python] - Python can't be executed by cron

#6 Post by donald »

@guilherme.stefani If you could re-enter your code using the [ code ] tag it may be easier for others to read the syntax.

Code: Select all

 
  words word word wordy word
 
Typo perfectionish.


"The advice given above is all good, and just because a new message has appeared it does not mean that a problem has arisen, just that a new gremlin hiding in the hardware has been exposed." - FreewheelinFrank

Valter
Posts: 4
Joined: 2022-09-11 10:37
Location: Europe
Has thanked: 2 times

Re: [Python] - Python can't be executed by cron

#7 Post by Valter »

Hello, most problems with crontab are environment variables unset.
guilherme.stefani wrote: 2022-12-21 17:30

Code: Select all

# grep CRON /var/log/syslog
Dec 21 00:15:01 srv-debian CRON[5263]: (root) CMD (/usr/local/bin/python3.8 /dados/python_script/1.Rotinas_Automatizadas/Alertas/BI.py)
Dec 21 00:15:02 srv-debian CRON[5262]: (CRON) info (No MTA installed, discarding output)
This just say that you job has been run (with or without errors). Errors from cron jobs are not logged to syslog but mail. Since there is no MTA on your system, you should add you own log to debug:

Code: Select all

*/2 * * * * /usr/local/bin/python3.8 /dados/python_script/1.Rotinas_Automatizadas/Alertas/BI.py > /tmp/BI.py.log 2>&1
Regards

MCaspers
Posts: 4
Joined: 2023-02-01 13:21

Re: [Python] - Python can't be executed by cron

#8 Post by MCaspers »

I notice your python script has a shebang in it.
If the file were an executable (chmod +x) you could just start it as such (./BI.py) without the need to provide python3.8 on the cli or in your croj.

Post Reply