This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rudimentary wrapper to run plexWatch as a daemon
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |