-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimv
executable file
·88 lines (70 loc) · 1.76 KB
/
imv
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
#!/usr/bin/perl
# abstract: watch a dir and scp all created files therein to another host
use strict;
use Linux::Inotify2;
use Getopt::Long;
my $DEBUG = 0;
my $dest = '/mnt/Music_3/.torrents/';
my $log = "$ENV{HOME}/.mvtorrents.log";
my $pidfile = '/tmp/mvtorrents.pid';
GetOptions(
k => \&killkid,
d => \$DEBUG,
);
my $what = shift // "$ENV{HOME}/TorrentsToServer";
if(!-d $what) {
print STDERR "$what is not a directory, bye\n";
exit(1);
}
daemonize($log) unless($DEBUG);
watch($what);
sub watch {
my $dir = shift;
my $n = Linux::Inotify2->new;
$n->watch($dir, IN_CREATE) or die($!);
while(my @events = $n->read) {
for my $event(@events) {
sleep 2;
if($event->{name} =~ m/\.torrent$/) {
print localtime(time) . "\tIN_CREATE\t$event->{name} => $dest\n";
system("scp -P 19216 $what/$event->{name} scp1\@192.168.1.128:$dest");
}
}
}
return 0;
}
sub daemonize {
my $daemon_log = shift // '/dev/null';
use POSIX 'setsid';
my $PID = fork();
exit(0) if($PID); #parent
exit(1) if(!defined($PID)); # out of resources
setsid();
$PID = fork();
exit(1) if(!defined($PID));
if($PID) { # parent
waitpid($PID, 0);
unlink($pidfile); # remove the lock when child have died
exit(0);
}
elsif($PID == 0) { # child
open(my $fh, '>', $pidfile) or die("Cant open $pidfile: $!");
print $fh $$;
close($fh);
open(STDOUT, '>', $daemon_log);
open(STDERR, '>', '/dev/null');
open(STDIN, '<', '/dev/null');
}
}
sub killkid {
open(my $fh, '<', $pidfile) or print "imv is not running\n" and exit(1);
my $target = <$fh>;
close($fh);
if(kill(9, $target)) {
print "imv with PID $target terminated\n";
}
else {
print "Could not kill $target: $!";
}
exit(0);
}