added init script; run as daemon by default
authorMatthias Förste foerste@schlittermann.de
Thu, 03 May 2012 13:33:14 +0200
changeset 19 a9b77650cbc4
parent 18 8d116efc6867
child 20 e34d1da2a45c
added init script; run as daemon by default
.hgignore
Makefile
rc.wgnd-watch.in
wgnd-watch.pl
--- a/.hgignore	Mon Apr 30 12:57:15 2012 +0200
+++ b/.hgignore	Thu May 03 13:33:14 2012 +0200
@@ -1,5 +1,5 @@
 syntax: re
-^wgnd-watch
+^(rc\.)?wgnd-watch
 ^wgnd-watch.map.pl
 ^debian/files$
 ^debian/wgnd-watch[./]
--- a/Makefile	Mon Apr 30 12:57:15 2012 +0200
+++ b/Makefile	Thu May 03 13:33:14 2012 +0200
@@ -8,12 +8,17 @@
 mandir		=	$(datarootdir)/man
 sbinscripts	=	$(package)
 
+rcdir		= 	/etc/init.d
+
 %: %.pl
 	cp -a $< $@
 %.1: %.pl
 	pod2man $< $@
 
-all: $(package) $(package).1
+rc.%: rc.%.in
+	perl -pe 's{__sbindir__}{${sbindir}}g' <$< >$@
+
+all: $(package) $(package).1 rc.$(package)
 
 install: all
 	install -m 0755 -d $(DESTDIR)$(sbindir)
@@ -22,6 +27,8 @@
 	install -m 0755 $(package).map.pl.ex $(DESTDIR)$(docdir)
 	install -m 0755 -d $(DESTDIR)$(mandir)/man1
 	install -m 0755 $(package).1 $(DESTDIR)$(mandir)/man1
+	install -m 0755 -d $(DESTDIR)$(rcdir)
+	install -m 0755 rc.$(package) $(DESTDIR)$(rcdir)/$(package)
 
 clean:
-	-rm -f $(package) $(package).1
+	-rm -f $(package) $(package).1 rc.$(package)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rc.wgnd-watch.in	Thu May 03 13:33:14 2012 +0200
@@ -0,0 +1,38 @@
+#! /bin/sh 
+
+### BEGIN INIT INFO
+# Provides:          wgnd-watch
+# Required-Start:    $local_fs $remote_fs $syslog $named $network $time
+# Required-Stop:     $local_fs $remote_fs $syslog $named $network
+# Should-Start:
+# Should-Stop:
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Start/Stop the wgnd-watch daemon
+### END INIT INFO
+
+DAEMON=__sbindir__/wgnd-watch
+PIDFILE=/var/run/`basename $DAEMON`.pid
+
+case $1 in
+	
+	start)	
+		start-stop-daemon -v --start --pidfile $PIDFILE --startas $DAEMON
+		;;
+		
+	stop)	
+		start-stop-daemon -v --stop --retry 30 --pidfile $PIDFILE
+		;;
+		
+	restart) echo "Restarting $NAME..."
+		$0 stop
+		$0 start
+		;;
+
+	*)
+		echo "Usage: $0 {start|stop|restart}" >&2
+		exit 1			
+		;;
+esac
+
+exit 0
--- a/wgnd-watch.pl	Mon Apr 30 12:57:15 2012 +0200
+++ b/wgnd-watch.pl	Thu May 03 13:33:14 2012 +0200
@@ -25,17 +25,17 @@
 
 my $VERSION = '0.1';
 my $ME = $0;
+
+use File::Basename;
+# File::Rsync in squeeze does not support --xattrs yet
+#use File::Rsync;
 use Getopt::Long;
 use Pod::Usage;
 use Linux::Inotify2;
-# File::Rsync in squeeze does not support --xattrs yet
-#use File::Rsync;
-
-my $map = 'wgnd-watch.map.pl';
+use Sys::Syslog;
 
 BEGIN {
 
-    use Sys::Syslog;
     openlog($ME, 'ndelay,nowait,pid', 'LOG_USER');
     $SIG{__WARN__} = sub {
         warn @_ if not defined $^S;
@@ -48,16 +48,25 @@
         syslog('err', "@_");
         exit $?;
     };
-
     sub dolog { print "@_"; syslog('info', "@_"); }
     sub trace { print @_ if $ENV{DEBUG}; }
 
+    use sigtrap qw(die normal-signals);
+
 }
 
+my %opts = (
+    daemon => 1,
+    map =>  'wgnd-watch.map.pl',
+    pidfile => '/var/run/wgnd-watch.pid'
+);
+
 GetOptions(
-    "map=s"  => \$map,
-    "h|help" => sub { pod2usage( -verbose => 0, -exitval => 0 ) },
-    "m|man"  => sub {
+    "map=s"  => \$opts{map},
+    "daemon!" => \$opts{daemon},
+    "pidfile=s" => \$opts{pidfile},
+    "h|help!" => sub { pod2usage( -verbose => 0, -exitval => 0 ) },
+    "m|man!"  => sub {
         pod2usage(
             -verbose   => 2,
             -exitval   => 0,
@@ -68,10 +77,35 @@
 
 our $source;
 use lib ('.', $ENV{HOME}, '/etc'); 
-require $map;
+require $opts{map};
+
+$0 = "$ME @ARGV";
+
+chdir("/") or die "Can't chdir to /: $!\n";
+
+if ($opts{daemon}) {
+
+    open STDIN,  "</dev/null" or die "Can't redir STDIN: $!\n";
+    open STDOUT, ">/dev/null" or die "Can't redir STDOUT: $!\n";
+
+    defined (my $pid = fork) or die "Can't fork: $!\n";
+    if ($pid) {
+        dolog "Child is [$pid]";
+        exit 0;
+    }
+    POSIX::setsid()
+        or die "Can't setsid: $!\n";
+    open(STDERR, ">&STDOUT") or die "Can't dup stdout: $!\n";
+
+    open(P, '>', $opts{pidfile})
+        or die "Can't open '>', '$opts{pidfile}': $!\n";
+    print P $$;
+    close P;
+
+}
 
 my $inotify = new Linux::Inotify2
-    or die "Can't create new inotify object: $!";
+    or die "Can't create new inotify object: $!\n";
 my @rsync = qw(/usr/bin/rsync -ihv -aAX);
 
 for (keys %{$source}) {
@@ -102,13 +136,17 @@
     $inotify->poll;
     while (-1 != (my $pid = wait)) {
         my $e = $? >> 8;
-        dolog "child ${ME}[$pid]: exit $e\n";
+        dolog "Child ${ME}[$pid]: exit $e\n";
     }
 
 }
 
 END {
+
+    unlink $opts{pidfile} if $opts{daemon};
+    dolog "exit";
     closelog;
+
 }
 
 __END__
@@ -135,11 +173,20 @@
 
 =over
 
+=item B<--[no]daemon>
+
+[Don't] run as a daemon. Default: do.
+
 =item B<--map> I<filename>
 
 Name of a file containing mappings between templates and directories. Defaults
 to F<wgnd-watch.map.pl>.
 
+=item B<--pidfile> I<filename>
+
+Location of the pid file. This is ignored unless we are running as a daemon.
+Defaults to F</var/run/wgnd-watch.pid>.
+
 =back
 
 =head1 FILES
@@ -150,6 +197,10 @@
 
 default for B<--map>
 
+=item F</var/run/wgnd-watch.pid>
+
+default for B<--pidfile>
+
 =back
 
 =head1 AUTHOR