0

In order to detect a fault that would happen in my cron job, I set an other cron job to check the previous log issued by the crontab:

def check_last_cron():
    with open('/home/pi/Desktop/cron_output.log') as f:
        txt = f.read()
        f.close()
        if 'Traceback' in txt:
            print('Traceback detected')
            send_to_phone(txt)
        else:
            print('no Traceback detected')

When run manually, everything goes well:

  • the script reads
  • detects the word "Traceback"
  • sends me the log

Crontab:

*/5 * * * * python3 /home/pi/Desktop/initialize.py > /home/pi/Desktop/cron_output.log 2>&1
*/5 * * * * python3 /home/pi/Desktop/checker.py > /home/pi/Desktop/log.log 2>&1

But when run with the crontab, the log read is empty. What am I missing ?

Gettippi
  • 5
  • 3

3 Answers3

0

I'm not sure at all but maybe here are 2 things that you could try :

  • Instead of using 2 cron jobs, you could just do :
    */5 * * * * python3 /home/pi/Desktop/initialize.py > /home/pi/Desktop/cron_output.log 2>&1 && python3 /home/pi/Desktop/checker.py > /home/pi/Desktop/log.log 2>&1

So that it will execute after the first command

  • Maybe add sudo to your second command, it might not have the rights to read the logs (seems unlikely but who knows)
    sudo python3 /home/pi/Desktop/checker.py > /home/pi/Desktop/log.log 2>&1
Log1k
  • 95
  • 6
0

I am not sure why would you run both scripts at the same time. the checker.py file supposed to run after initialize.py.

try this ->

*/5 * * * * python3 /home/pi/Desktop/initialize.py > /home/pi/Desktop/cron_output.log 2>&1

*/6 * * * * python3 /home/pi/Desktop/checker.py > /home/pi/Desktop/log.log 2>&1

plus :- whats you are getting currently in log.log file ? is it no Traceback detected. ?

0

It seems like the previous cron log is erased every time the cron job is restarted.

Adding another cron job to check the first log is working but only by executing it with a time offset as below:

*/5 * * * * python3 /home/pi/Desktop/initialize.py > /home/pi/Desktop/cron_output.log 2>&1
1-59/5 * * * * python3 /home/pi/Desktop/checker.py > /home/pi/Desktop/log.log 2>&1

This 1-59/5 is executing checker.py every 5 minutes, one minute before initialize.py as explained in this post

Gettippi
  • 5
  • 3