-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestartinator.pl
executable file
·91 lines (79 loc) · 1.9 KB
/
restartinator.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env perl
#TODO - Restart on normal exit?
#TODO - Signal handling
#TODO - STDOUT, STDERR
#TODO - exclude from .ignore file
#TODO - recurse in directory
use File::Monitor;
use File::Monitor::Object;
use Proc::Simple;
use Getopt::Long;
Getopt::Long::Configure('bundling');
my $debug = 0;
my $verbose = 0;
my @files = ( );
my $options = GetOptions(
"debug!" =>\$debug,
"v|verbose:+" =>\$verbose,
"w|watch=s" =>\@files
);
@files = ('.') unless $files[0];
#TODO - get command from remainder of ARGS
my @cmdargs = @ARGV;
unless ($ARGV[0]) {
die "Usage $0 [ --debug ] [ --verbose ] [ --watch PATH ] -- COMMAND ARGS\n";
}
print STDERR ("ARGV: ",join('|',@ARGV),"\n") if $debug;
my $command = join(' ',@cmdargs);
# Proc::Simple::debug($debug);
my $proc = Proc::Simple->new();
my $monitor = File::Monitor->new();
print STDERR ("Watching ",join(":",@files),"\n") if $debug;
for my $file ( @files ) {
$monitor->watch ( {
name => $file,
recurse => 1
});
}
#Initial Scan
$monitor->scan;
my $wannarun = 1;
my $failed = 0;
my $running = 0;
my $failure_reported = 0;
#Start Process
if ($proc->start(@cmdargs)) {
print "Started Command: \"$command\" with pid: ".$proc->pid."\n";
}else{
die "exit_status=".$proc->exit_status."\n";
}
#After time
while ($wannarun) {
print "Checking\n" if $debug;
my $changed = 0;
for my $change ( $monitor->scan ) {
print "Changed!\n" if $debug;
$changed = 1;
}
$running = $proc->poll();
if ($changed) {
print "Change detected!\n";
if ($running || $failed){
$proc->kill() if $running;
$proc->start(@cmdargs);
print "Started Command: \"$command\" with pid: ".$proc->pid."\n";
$failure_reported = 0;
}
}else{
if (! $running){
$failed = $proc->exit_status;
if ( $failed == 0 ) {
$wannarun = 0;
}else{
print "Abnormal exit: $failed\nWaiting for change before restarting" unless $failure_reported;
$failure_reported = 1;
}
}
}
sleep 2;
}