#!/usr/bin/perl use v5.36; use warnings; use strict; use Time::HiRes qw(sleep); use Linux::Inotify2; use POSIX qw(strftime WIFSIGNALED WTERMSIG SIGINT); use Mojo::File; use Y::AoC qw(grey red); my $cmd = shift; my $backup = "backup/$cmd"; $cmd = "./$cmd" if $cmd !~ /\//; my $last_backup; my $file = Mojo::File->new($cmd); my $mtime; while (1) { my $b = "$backup-".strftime("%H-%M-%S", localtime(time)); $file->copy_to($b); $mtime = $file->stat->mtime; say grey("\nrunning $cmd @ARGV... ============================"); system $cmd, @ARGV; if ($? && WIFSIGNALED($?) && WTERMSIG($?) == SIGINT) { say red(" Got SIGINT"); if ($file->stat->mtime > $mtime) { next; } } elsif ($?) { say grey("FAILED: $?"); } else { say grey("finished OK"); } my $inotify = Linux::Inotify2->new; $inotify->watch($cmd, IN_MODIFY); say grey("\nWaiting for modification of $cmd ..."); $inotify->read; sleep 0.1; }