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); }

In reply to Perl Daemon Calling a script by perlology

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.