forked from ljunkie/plexWatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plexWatch.notifyDeamon.pl
executable file
·49 lines (40 loc) · 1.04 KB
/
plexWatch.notifyDeamon.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
#!/usr/bin/perl
use strict;
use POSIX qw(setsid);
use Fcntl qw(:flock);
my $plexWatch_script = '/opt/plexWatch/plexWatch.pl';
###########################################################################
my $debug = 0;
my $script_fh;
&CheckLock;
chdir '/';
umask 0;
#open STDIN, '/dev/null';
#open STDERR, '>/dev/null';
#open STDOUT, '>/dev/null';
defined(my $pid = fork);
exit if $pid;
close STDIN;
close STDOUT;
close STDERR;
setsid or die "Can't start a new session: $!";
umask(0027); # create files with perms -rw-r-----
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '<', '/dev/null' or die $!;
open STDOUT, '>', '/dev/null' or die $!;
open STDERR, '>>', '/tmp/plexWatch.log';
setsid;
while(1) {
sleep(5);
my $cmd = $plexWatch_script;
$cmd .= ' ' . join(' ',@ARGV) if @ARGV;
system($cmd);
}
sub CheckLock {
open($script_fh, '<', $0)
or die("Unable to open script source: $!\n");
while (!flock($script_fh, LOCK_EX|LOCK_NB)) {
print "$0 is already running. waiting.\n" if $debug;
exit;
}
}