#!/usr/bin/perl # ---------------------------------------------------------------------- # EYEBREAK -- PERL PROGRAM # ---------------------------------------------------------------------- # This program will display a dialog every 20 minutes to remind you # to take a break. # ---------------------------------------------------------------------- $opt_message = "Time to take a break!"; $backtowork = "O.K. Back to work."; $breaktime = 20 * 60; $opt_pidfile = "$ENV{HOME}/.eyebreak.pid"; # ---------------------------------------------------------------------- # Use GetOptions to parse the command line options... use Getopt::Long; $autoabbrev=1; $result = GetOptions("message=s", "pidfile=s", "stop", "help", "debug"); if ($opt_help or !$result) { print "Just run the program, but keep in mind that it won't\n"; print "finish running. It will keep running and every 20 minutes\n"; print "pop up and annoying dialog that reminds you to take a break.\n"; exit; } # Turn this program into a daemon ... open STDIN, '/dev/null' or die "Can't read /dev/null: $!\n"; open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!\n"; open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!\n"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!\n"; if (-r "$opt_pidfile") { open (P, $opt_pidfile) or die "Can't read $opt_pidfile: $!\n"; my $otherPID =
;
close(P);
open (S, "ps -p $otherPID -o ucomm |");
my $ignore_header = ;
my $command_line = ;
close(S);
if ($command_line =~ /perl/) # We have a live process running
{
if ($opt_stop or $0 =~ /stop$/i)
{
kill "USR1", $otherPID;
}
else
{
kill "HUP", $otherPID; # Send the signal ...
# print "Sent the HUP signal to $otherPID ...\n";
}
exit;
}
}
$SIG{HUP}="restart";
$SIG{USR1}="stopping";
$SIG{INT}="stopping";
# Store our process ID where we can find it again.
open (P, ">$opt_pidfile") or die "Can't write to $opt_pidfile: $!\n";
print P $$;
close(P);
# ----------------------------------------------------------------------
# MAIN PROGRAM LOGIC
# ----------------------------------------------------------------------
use MacPerl;
use Mac::Speech;
SpeakString("Starting the eye break timer.");
$restarted = 0;
while (1) # Loop forever and ever
{
sleep $breaktime; # Sleep for 20 minutes until
# we get a break.
if ($restarted != 1)
{
SpeakString($opt_message); # Alert me with a sultry voice
MacPerl::Answer("$opt_message");
sleep 1 while SpeechBusy(); # Don't continue unless it has
# stopped speaking.
sleep 30; # Sleep for duration of our break
SpeakString($backtowork); # Tell me the break is over
sleep 1 while SpeechBusy(); # Wait for the voice to stop
}
$restarted = 0;
}
# This subroutine will be called if we are running and get
# a HUP signal to restart the timer.
sub restart
{
SpeakString("Restarting the timer");
sleep 1 while SpeechBusy(); # Wait for the voice to stop
$restarted = 1;
}
sub stopping
{
SpeakString("Stopping eye break");
sleep 1 while SpeechBusy(); # Wait for the voice to stop
unlink $opt_pidfile;
exit;
}