From b796d2d90768c09ad093f4727250e8357e21dc62 Mon Sep 17 00:00:00 2001 From: Kazuho Oku Date: Mon, 14 Dec 2009 16:31:26 +0900 Subject: [PATCH] initial import --- Makefile.PL | 13 +++++++++ restart_app | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 Makefile.PL create mode 100755 restart_app diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..352b7de --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,13 @@ +use inc::Module::Install; + +name 'App-Restarter'; +all_from 'restart_app'; +readme_from 'restart_app'; +license 'perl'; + +requires 'Filesys::Notify::Simple' => 0.05; + +install_script 'restart_app'; + +auto_include; +WriteAll; diff --git a/restart_app b/restart_app new file mode 100755 index 0000000..4a9cb26 --- /dev/null +++ b/restart_app @@ -0,0 +1,82 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use POSIX qw(:sys_wait_h SIGTERM); +use Getopt::Long; +use Filesys::Notify::Simple; +use Pod::Usage qw/pod2usage/; + +our $VERSION = '0.01'; + +# ------------------------------------------------------------------------- +# configuration part + +my $restart = ['.']; +my $signal = SIGTERM; # SIGTERM + +GetOptions( + 'r|restart=s' => sub { $restart = [split /,/, $_[1]] }, + 's|signal' => \$signal, + 'h|help' => \my $help, +) or pod2usage(0); +pod2usage(1) if $help; + +# ------------------------------------------------------------------------- +# code part + +&main; exit; + +sub main { + print "watching " . join(", ", @$restart) . "\n"; + my @child = @ARGV; + my $watcher = Filesys::Notify::Simple->new($restart); + my $pid = spawn_child(@child); + while (1) { + $watcher->wait(sub { + for my $event (@_) { + print "-- $event->{path} was changed\n"; + } + kill $signal, $pid; + waitpid $pid, -1; + $pid = spawn_child(@child); + }); + } +} + +sub spawn_child { + my @child = @_; + + if (my $pid = fork()) { + return $pid; # parent(watcher) + } elsif ($pid == 0) { + exec @child; # child(application) + + die "cannot reach here"; + } else { + die "cannot fork: $!"; + } +} + +__END__ + +=head1 NAME + +restart_app - restarts the command when files change + +=head1 SYNOPSIS + + $ restart_app -r lib,config -- your other command + +=head1 AUTHOR + +Kazuho Oku + +written by tokuhirom + +=head1 LICENSE + +This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10. + +=cut