--- a/tele-watch.pl Sun Mar 01 12:49:52 2009 +0100
+++ b/tele-watch.pl Sun Mar 01 12:50:22 2009 +0100
@@ -1,5 +1,24 @@
#! /usr/bin/perl
-# (c) Heiko Schlittermann <hs@schlittermann.de>
+#
+# Script to watch directories and to perform some actions on
+# changes, highly specialized on DTELE.
+#
+# Copyright (C) 2009 Heiko Schlittermann
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Heiko Schlittermann <hs@schlittermann.de>
use strict;
use warnings;
@@ -12,11 +31,14 @@
use File::Temp qw(tempfile);
use POSIX qw(setsid);
-my $ME = basename $0;
+my $ME = basename $0;
+my $VERSION = "0.1";
my $opt_block = 1;
my $opt_daemon = 1;
my $opt_pidfile = "/var/run/$ME.pid";
+my $opt_kill = 0;
+my $opt_version = 0;
sub writef($@);
sub updatef($@);
@@ -25,12 +47,61 @@
sub timestamp();
sub dir($);
-openlog($ME, LOG_PID | LOG_PERROR, LOG_DAEMON);
-
MAIN: {
my @_ARGV = @ARGV;
my %TARGET;
+ GetOptions(
+ "h|help" => sub { pod2usage(-exitval => 0, -verbose => 1) },
+ "m|man" => sub { pod2usage(-exitval => 0, -verbose => 3) },
+ "block!" => \$opt_block,
+ "daemon!" => \$opt_daemon,
+ "kill" => \$opt_kill,
+ "pidfile=s" => \$opt_pidfile,
+ )
+ and @ARGV
+ or pod2usage();
+
+ if ($opt_kill) {
+ die "$ME: Not killing anything, no pid file.\n";
+ my $pid = readf($opt_pidfile);
+ die "$ME: not killing anything, no pid.\n";
+ kill TERM => $pid
+ or die "$ME: can't kill $pid: $!\n";
+ print "$ME: sent TERM signal to $pid\n";
+ exit 0;
+ }
+
+ if ($opt_version) {
+ print "$ME:\n$VERSION";
+ exit 0;
+ }
+
+ foreach (@ARGV) {
+ my ($w, $t, $r) = split /:/;
+ die "$ME: too many \":\" in \"$_\"\n" if defined $r;
+ pod2usage() if not defined $w or not defined $t;
+ $w = abs_path($w);
+ $t = abs_path($t);
+ $TARGET{$w} = $t;
+ }
+
+ writef($opt_pidfile, $$) if $opt_pidfile;
+
+ openlog($ME, LOG_PID | LOG_PERROR, LOG_DAEMON);
+ $SIG{INT} = sub { syslog(LOG_NOTICE, "got signal @_"); exit 0 };
+ $SIG{TERM} = $SIG{INT};
+ $SIG{__WARN__} = sub {
+ warn @_ if not defined $^S;
+ syslog(LOG_WARNING, "%s", "@_");
+ };
+ $SIG{__DIE__} = sub {
+ die @_ if not defined $^S;
+ syslog(LOG_ERR, "%s", "@_");
+ exit $?;
+ };
+
+ # cleanup code
END {
foreach (keys %TARGET) {
if (readf("$_/.watched") == $$) {
@@ -39,42 +110,12 @@
syslog(LOG_NOTICE, "cleaned $_/.watched");
}
}
- unlink $opt_pidfile if $opt_pidfile and readf($opt_pidfile) == $$;
+ unlink $opt_pidfile
+ if $opt_pidfile
+ and defined($_ = readf($opt_pidfile))
+ and $_ == $$;
}
- $SIG{INT} = sub { syslog(LOG_NOTICE, "got signal @_"); exit 0 };
- $SIG{TERM} = $SIG{INT};
- $SIG{__DIE__} = sub {
- die @_ if not defined $^S;
- syslog(LOG_ERR, "%s", "@_");
- exit $?;
- };
- $SIG{__WARN__} = sub {
- warn @_ if not defined $^S;
- syslog(LOG_WARNING, "%s", "@_");
- };
-
- GetOptions(
- "h|help" => sub { pod2usage(-exitval => 0, -verbose => 1) },
- "m|man" => sub { pod2usage(-exitval => 0, -verbose => 3) },
- "block!" => \$opt_block,
- "daemon!" => \$opt_daemon,
- "pidfile=s" => \$opt_pidfile,
- )
- and @ARGV
- or pod2usage();
-
- foreach (@ARGV) {
- my ($w, $t, $r) = split /:/;
- die "too many \":\" in \"$_\"\n" if defined $r;
- pod2usage() if not defined $w or not defined $t;
- $w = abs_path($w);
- $t = abs_path($t);
- $TARGET{$w} = $t;
- }
-
- writef($opt_pidfile, $$) if $opt_pidfile;
-
# mark the directories as watched
foreach (keys %TARGET) {
my $watcher = readf("$_/.watched");
@@ -321,11 +362,28 @@
The name of the file holding the pid of the running process. (default:
/var/run/tele-watch.pid)
+=item B<--kill>
+
+This is actually no option, it can be used to kill a running process.
+It just reads the PID from the I<pidfile> (see above) and sends a TERM
+signal. (default: off)
+
+=item B<--version>
+
+Print the version information and exit.
+
=back
-=head1 AUTHOR
+=head1 COPYRIGHT
+
+GPL, see source.
-Heiko Schlittermann <hs@schlittermann.de>
+=head1 AUTHOR and SOURCE
+
+Heiko Schlittermann <hs@schlittermann.de>.
+The source may be found at L<https://keller.schlittermann.de/hg/ius/tele-watch>.
+
+<VERSION>
=cut