Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
rudimentary wrapper to run plexWatch as a daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
ljunkie committed May 29, 2014
1 parent 5bfea4c commit e05ab13
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions plexWatch.notifyDeamon.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}

0 comments on commit e05ab13

Please sign in to comment.