Category: | Utility Scripts |
Author/Contact Info | Tex Thompson tex@biosysadmin.com |
Description: | Helios will monitor a file (or set of files) for changes to their modification times, and execute a command (or list of commands) after the modification time is updated. Inspired by the atchange program available from http://www.lecb.ncifcrf.gov/~toms/atchange.html. I use this program in order to save myself some keystrokes while compiling java files:
$ helios --verbose StringSort.java 'javac *.java' Watching StringSort.java, initial mtime is 1076448240 StringSort.java last modified at 1076524024. Executing javac *.java ... done. Or, to save myself even more keystrokes: $ helios --multiple-cmds StringSort.java 'javac *.java','java StringSort' It's fun to watch the output scroll by after just saving my file. :) |
#!/usr/bin/perl -w
use strict;
use File::Basename;
use Getopt::Long;
use Time::HiRes qw ( sleep );
use Pod::Usage;
$0 = basename $0;
my ($time,$file,$cmd,$verbose,$help,$many_cmds);
# read in the command line options
GetOptions( 'time=f' => \$time,
'multiple-cmds' => \$many_cmds,
'help' => \$help,
'verbose' => \$verbose ) || pod2usage(2);
# print a help message if requested
if ( $help ) {
pod2usage( -verbose => 2 );
}
# exit with a help message if called incorrectly
if ( !$ARGV[0] || !$ARGV[1] ) {
pod2usage(2);
}
# correctly split up the files and commands
my ( @files,@cmds );
if ( $many_cmds ) {
@cmds = split /,/, (pop @ARGV);
} else {
@cmds = pop( @ARGV );
}
@files = @ARGV;
# correctly set $time if not set from the command line
if ( !$time ) { $time = 0.25 };
$SIG{'INT'} = \&catch_sigint;
my %modtime; # keeps track of file modification times for each file
foreach my $file (@files) {
$modtime{ $file } = (stat($file))[9];
if ( $verbose ) {
print "Watching $file, initial mtime is $modtime{$file}\n";
}
}
if ( $new != $modtime{$file} ) {
$modtime{$file} = $new;
if ( $verbose ) {
print "$file last modified at $modtime{$file}.\n"
} ;
foreach my $cmd (@cmds) {
print "Executing $cmd ... ";
system( $cmd );
print "done.\n";
}
}
} }
###############
# Subroutines #
###############
sub catch_sigint { print STDERR "\nExiting $0 ...\n"; exit 1; }
#####################
# Usage Information #
#####################
=head1 NAME
helios - automatic file monitoring and command execution
=head1 SYNOPSIS
helios [options] file1 [file2 ...] command(s)
options: --time number_of_seconds --verbose --multiple-cmds --help
=head1 OPTIONS
=item B<--multiple-cmds>
Indicates that a comma-separated list of commands is supplied on the
command line. Each command will be executed in succession after a
file is modified.
=item B<--time>
Sets the time (in seconds) between checks of the filename. Defaults to
1 second. Warning: accurate monitoring of time less than a second is
not guaranteed, see the Time::HiRes manpage fmodification times, and t
+he commands executed.
=head1 EXAMPLES
# Monitor HelloWorld.java and HellWorld2.java, execute 'javac *.java'
# when either of these has changed
$ helios HelloWorld.java HelloWorld2.java 'javac *.java'
# Monitor myFile.java, execute the commands 'javac *.java' and 'java
# myFile' when the file has changed.
$ helios --multiple-cmds myFile.java 'javac *.java','java myFile'
# Monitor HelloWorld.java every 0.25 seconds, execute 'javac *.java
# when the file has changed.
$ helios --time 0.25 --verbose HelloWorld.java 'javac *.java'
=head1 DESCRIPTION
B<helios> will monitor a file for changes and execute a command
whenever that file changes. It is named after the Greek god Helios,
the god of sun and sight.
While multiple files can be separated by spaces, commands should
always be grouped into a single
Development of B<helios> was inspired by the atchange program,
originally by Dr. Tom Schneider:
<http://www.lecb.ncifcrf.gov/~toms/atchange.html>.
B<helios> has a few advantages over the original atchange program:
- Support for monitoring of multiple files
- Support for executing multiple commands from the command-line
- Support for changing the polling time of files
- Better platform independence (less reliance on csh)
=head1 AUTHOR
Tex Thompson <tex@biosysadmin.com>
=head1 LICENSE
B<helios> is licensed under the GNU GPL license, available from
http://www.gnu.org/.
=cut
|
|
---|