views:

25

answers:

2

I'm in charge of a printer, so I wrote a script which runs every 5 minutes and figures out if the printer has paper. If it doesn't, the script will text me. The problem is, if I'm busy, and can't fill the printer, I don't want the script to continue to text me every 5 minutes. Is there a way I can force it to only send me at most 1 text every 8 hours or so, to ensure that the script doesn't text me twice for the same out-of-paper situation? The only thing I can currently think of is to create a db of times that I get texts, then make sure that the most recent one wasn't too long ago, or to create a local file with the most recent time in it.

Thanks!

+2  A: 

You need to store somewhere the fact that it has text you and when this last occurred. You could do this using a plain file and by reading the files modification date to see when the text was last sent or you can use a database.

jakenoble
-1: using a variable name complicates storing of data the system already has in the file's modification time
symcbean
@symcbean Good point- - edited
jakenoble
A: 

Assuming that the script that sends the SMS is PHP, use something like this. Can probably find a cleaner way of doing this, but this is just to show you the logic that is needed.

<?

/*
 * Replace outOfPaper() / sendSms() with the actual logic of your script
 */
$statusFile = './lastsms';

if(outOfPaper() && (is_file($statusFile) && (filemtime($statusFile) < time()-((8*60)*60)))){
    sendSms('+4412345678','Printer out of paper');
    touch($statusFile);
}

?>
Ollie