ppyhd
ppyhd

Reputation: 35

How to sleep a python script running as a cronjob?

I wrote a python script to monitor a log file on a CentOS server for a specific value and send an email when it finds it. It runs as a cron every 5 minutes.

My question is what is the best way to put this script to sleep after it has sent the first email. I don't want it to be sending emails every 5 mins, but it needs to wake up and check the log again after an hour or so. This is assuming the problem can be fixed in under an hour. The people who are receiving the email don't have shell access to disable the cron.

I thought about sleep but I'm not sure if cron will try to run the script again if another process is active (sleeping).

Upvotes: 1

Views: 2438

Answers (6)

zie
zie

Reputation: 730

Since you are monitoring a log file, It might be worth checking into things that already do log file monitoring. Logwatch is one, but there are log analyzing tools, that handle all of these things for you:

http://chuvakin.blogspot.com/2010/09/on-free-log-management-tools.html

Is a good wrap-up of some options. They would handle yelling at people. Also there are system monitoring tools such as opennms or nagios, etc. They also do these things.

I agree with what other people have said above, basically cron ALWAYS runs the job at the specified time, there is a tool called at which lets you run jobs in the future, so you could batch a job for 5 minutes, and then at runtime decide, when do I need to run again, and submit a job to at for whatever time you need it to run again (be it 5 minutes, 10 minutes or an hour). You'd still need to keep state somewhere (like what @infrared said) that would figure out what got sent when, and if you should care some more.

I'd still suggest using a system monitoring tool, which would easily grow and scale and handles people being able to say 'I'm working on XX NOW stop yelling at me' for instance.

Good luck!

Upvotes: 0

Pat Daburu
Pat Daburu

Reputation: 446

Another way to go about this might be to run your script as a daemon and, instead of having cron run it every five minutes, put your logic in a loop. Something like this...

while True:
  # The check_my_logfile() looks for what you want.
  # If it finds what you're looking for, it sends
  # an email and returns True.
  if check_my_logfile():
    # Then you can sleep for 10 minutes.
    time.sleep(600)
  # Otherwise, you can sleep for 5 minutes.
  else:
    time.sleep(300)

Upvotes: 0

infrared
infrared

Reputation: 3626

When your scripts sends email, make it also create a txt file "email_sent.txt". Then make it check for existence of this txt file before sending email. If it exists, don't send email. If it does not exist, send email and create the text file.

The text files serves as an indicator that email has already been sent and it does not need to be sent again.

Upvotes: 1

Hugh Bothwell
Hugh Bothwell

Reputation: 56714

@Lennart, @S. Lott: I think the question was somewhat the other way around - the script runs as a cron job every five minutes, but after sending an error-email it shouldn't send another for at least an hour (even if the error state persists).

The obvious answer, I think, is to save a self-log - for each problem detected, an id and a timestamp for the last time an email was sent. When a problem is detected, check the self-log; if the last email for this problem-id was less than an hour ago, don't send the email. Then your program can exit normally until called again by cron.

Upvotes: 3

Lennart Regebro
Lennart Regebro

Reputation: 172437

You are running it every five minutes. Why would you sleep it? Just exit. If you want to make sure it doesn't send email every five minutes, then make the program only send an email if there is anything to send.

If you sleep it for an hour, and run it every five minutes, after an hour you'll have 12 copies running (and twelve emails sent) so that's clearly not the way to go forward. :-)

Upvotes: 0

S.Lott
S.Lott

Reputation: 392070

cron will absolutely run the script again. You need to think this through a little more carefully than just "sleep" and "email every 10 minutes."

You need to write out your use cases.

  1. System sends message and user does something.

  2. System sends message and user does nothing. Why email the user again? What does 2 emails do that 1 email didn't do? Perhaps you should SMS or email someone else.

How does the user register that something was done? How will they cancel or stop this cycle of messages?

What if something is found in the log, an email is sent and then (before the sleep finishes) the thing is found again in the log. Is that a second email? It is two incidents. Or is that one email with two incidents?

Upvotes: 4

Related Questions