Skip to content

Instantly share code, notes, and snippets.

@angstbear
Last active November 18, 2016 23:12
Show Gist options
  • Save angstbear/e83202c54dc456c28912450c1c76a542 to your computer and use it in GitHub Desktop.
Save angstbear/e83202c54dc456c28912450c1c76a542 to your computer and use it in GitHub Desktop.
Monitors file for change, and uploads to remote destination when change is detected.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use File::Monitor;
use Getopt::Long;
__PACKAGE__->new->run;
exit;
sub new {
my $class = shift;
my %args;
GetOptions(
"file=s" => \$args{'file'},
"dest=s" => \$args{'dest'},
"time=i" => \$args{'time'},
) or die 'Problem getting arguments';
$args{'time'} ||= 5;
die 'Missing argument' unless $args{'file'} and $args{'dest'};
return bless \%args, $class;
}
sub run {
my $self = shift;
my $monitor = File::Monitor->new;
$monitor->watch( $self->{'file'} );
say '-' x 50;
say "Monitoring: $self->{'file'}\nUpload to: $self->{'dest'}/";
say '-' x 50;
while (1) {
my @changes = $monitor->scan( $self->{'file'} );
if (@changes) {
my $cmd = "scp $self->{'file'} $self->{'dest'}/";
say "File $self->{'file'} changed. Uploading ... $cmd";
my $res = system("$cmd > /dev/null");
die "Upload failed. $?" if $res;
}
sleep $self->{'time'};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment