Python Script works fine but creates errors when run as cronjob
by jorjor242 from LinuxQuestions.org on (#538YP)
Hi all, I have a python script written to pull the last 200 logs from my suricata IDS alerts and email them to an email. The script works perfectly when run via CLI, but not as a cronjob set under sudo.
This is the Python code:
Code:import smtplib
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('xx', 'xx')
with open('fast.log', 'r') as file:
lines=file.readlines()
g = open('daylogs.txt', 'w+')
for i in range(1, 200):
g.write(lines[i*-1])
with open ('daylogs.txt', 'r') as file:
m = file.read()
#message = str(m)
#str.split(message)
#print(m)
smtpObj.sendmail('xx@gmail.com', 'xx@gmail.com', 'Subject: IDS ALERTS\n\n'+m)
smtpObj.quit()This is the cronjob:
* * * * * /usr/bin/python3 /var/log/suricata/pythonmail.py >>/var/log/surialertlog.txt 2>&1
This is the error:
Code:FileNotFoundError: [Errno 2] No such file or directory: 'fast.log'
Traceback (most recent call last):
File "/var/log/suricata/pythonmail.py", line 10, in <module>
with open('fast.log', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'fast.log'
Traceback (most recent call last):
File "/var/log/suricata/pythonmail.py", line 10, in <module>
with open('fast.log', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'fast.log'
Why doesn't it recognize the presence of fast.log? I've never had any errors opening fast.log when looking at suricata alerts...


This is the Python code:
Code:import smtplib
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('xx', 'xx')
with open('fast.log', 'r') as file:
lines=file.readlines()
g = open('daylogs.txt', 'w+')
for i in range(1, 200):
g.write(lines[i*-1])
with open ('daylogs.txt', 'r') as file:
m = file.read()
#message = str(m)
#str.split(message)
#print(m)
smtpObj.sendmail('xx@gmail.com', 'xx@gmail.com', 'Subject: IDS ALERTS\n\n'+m)
smtpObj.quit()This is the cronjob:
* * * * * /usr/bin/python3 /var/log/suricata/pythonmail.py >>/var/log/surialertlog.txt 2>&1
This is the error:
Code:FileNotFoundError: [Errno 2] No such file or directory: 'fast.log'
Traceback (most recent call last):
File "/var/log/suricata/pythonmail.py", line 10, in <module>
with open('fast.log', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'fast.log'
Traceback (most recent call last):
File "/var/log/suricata/pythonmail.py", line 10, in <module>
with open('fast.log', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'fast.log'
Why doesn't it recognize the presence of fast.log? I've never had any errors opening fast.log when looking at suricata alerts...