in reply to Reminder for KDE Kid

In your main loop, you check that the time difference is exactly 1800, 3600, etc. seconds. This is a condition that is only true for one second. And since you are sleeping for a full second, it seems conceivable that if the system is very busy, your process may not get woken up from its sleep in that one-second window of opportunity.

I might rewrite the loop in the following way to be sure that the action is triggered the first time the script is woken up after a target number of seconds.

sleep 1 until time - $llent->ll_time >= 1800; passive_pop("Thirty minutes"); sleep 1 until time - $llent->ll_time >= 3600; passive_pop("One hour"); sleep 1 until time - $llent->ll_time >= 5400; passive_pop("Ninety minutes"); sleep 1 until time - $llent->ll_time >= 7200; passive_pop("Two hours"); tattle_tail();
Or to be even fancier and extensible-er:
my %actions = ( 1800 => sub { passive_pop("Thirty minutes"); }, 3600 => sub { passive_pop("Sixty minutes"); }, 5400 => sub { passive_pop("Ninety minutes"); }, 7200 => sub { passive_pop("Two hours"); tattle_tail(); }, # 8000 => sub { disable_network_interface(); }, # 9999 => sub { self_destruct(); } ); for my $duration (sort {$a <=> $b} keys %actions) { sleep 1 until time - $llent->ll_time >= $duration; $actions{$duration}->(); }
Also, it appears that your code as written will loop forever. This may be fine if your KDE autostart things get automatically killed when he logs out, but is something to consider.

blokhead

Replies are listed 'Best First'.
Re^2: Reminder for KDE Kid
by KennV (Beadle) on Sep 22, 2007 at 04:57 UTC
    Thanks blokhead!!

    KDE's autostart thing did kill the script on logout but there was no reason to keep it running after the last action. So thanks again.

    I think maybe next time I've got some free time I'll put the actions in a config file using YAML::Tiny or something. Then I can put the actions in a readable file in my home folder and not have to worry about being root and all that to alter the actions. However, I don't think I should put code in the config, so I'll have to figure that out. Time to start reading...

    KennV
Re^2: Reminder for KDE Kid
by ruzam (Curate) on Oct 12, 2007 at 05:14 UTC
    Why sleep 1 second at all? No use hogging up the CPU looping every second when you can simply sleep until the next event.
    my $last = 0; for my $duration (sort {$a <=> $b} keys %actions) { sleep($duration - $last); $last += $duration; $actions{$duration}->(); }