perlology has asked for the wisdom of the Perl Monks concerning the following question:
I wish I could use Proc::Daemon, but I need to resort the creating my own daemon code. This daemon calls a script that performs a few input and output operations periodically. One of the input and outputs commands in the script is 'sed', using 'echo' to append to a file, using 'printf' to log to a logfile.
The 'sed' commands are not working from the script. One of the errors messages is bad file descriptors. The 'echo' commands are echoing too may lines.
Is best to not call a script and just rewrite the shell script using Perl?
Or am I fundamentally missing something in my Perl code in calling the shell script?
#!/usr/bin/perl -Tw # # use strict; use POSIX qw(setsid); use constant VERSION => 1.10; $ENV{'PATH'} = '/bin'; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; my $PID_FILENAME = "/var/run/one-user-only.pid"; my $ONE_USER_ONLY = "/usr/local/bin/one-user-only.sh"; # # If this file does exist, then just die accordingly # die("$PID_FILENAME exists!!") if ( -f $PID_FILENAME ); # # If this file does not exist, then just die accordingly # die("Cannot find $ONE_USER_ONLY") if ( ! -f $ONE_USER_ONLY ); sub daemonize { chdir "/"; # Close all default file descriptors open (STDIN, "</dev/null"); open (STDOUT, ">/dev/null"); open (STDERR, ">&STDOUT"); my $pid = fork (); # Write the PID to /var/run open (FD, ">$PID_FILENAME") or die("Cannot write PID: $!"); print FD "$$\n"; close(FD); # Detach from the terminal so interrupts cannot occur if ($pid < 0) { die "fork: $!"; } elsif ($pid) { exit 0; } # Make session leader setsid or die "setsid: $!"; umask 0; foreach (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024)) { POSIX::close $_ } } my $continue = 1; $SIG{TERM} = sub { unlink $PID_FILENAME; $continue = 0; }; &daemonize; while ($continue) { sleep 2; system($ONE_USER_ONLY); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl Daemon Calling a script
by roboticus (Chancellor) on Jan 24, 2013 at 01:56 UTC | |
by perlology (Initiate) on Jan 24, 2013 at 12:11 UTC | |
Re: Perl Daemon Calling a script
by Anonymous Monk on Jan 24, 2013 at 01:16 UTC | |
by perlology (Initiate) on Jan 24, 2013 at 01:52 UTC | |
by roboticus (Chancellor) on Jan 24, 2013 at 02:19 UTC | |
by perlology (Initiate) on Jan 24, 2013 at 16:32 UTC |
Back to
Seekers of Perl Wisdom