I was able to run this "sort of". I am having a bit of trouble with getting right module for use IPC::Run ('run'); so I just commented that out as well as the ping further down in the code.

Installation:

C:\Projects_Perl\testing>perl serva.pl install service name: serva Log file: C:\Projects_Perl\testing\serva.log Successfully added.
So far so good, I added printout to make sure service name and log file name were ok.

Now starting service:

C:\Projects_Perl\testing>net start serva The serva service is starting. The serva service could not be started. The service did not report an error. More help is available by typing NET HELPMSG 3534. C:\Projects_Perl\testing>type serva.log Fri May 5 15:57:50 2017 Exit Code 0 Run this service... Fri May 5 15:59:22 2017 Exit Code 0
The error reported from net start is that "serva" didn't start, but I can see from the serva.log file that it actually did something!

Of course, none of the print statements to the console, like print "Run ping...\n"; are going to do anything. You can only print LOG ... A Service (a daemon) does not any connection to a console. Not sure why it net says service didn't start, but then again, you stop yourself which is an odd thing for a service to do.

Update: I did look with the Service GUI. Installation looks fine. automatic start, currently stopped, no actions on failure. Error message for GUI "Start" had more info than I would have thought: "The serva service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example Performance Logs and Alerts service". Looks like the issue is with how you are running ping?

Update I am no sure why I couldn't get run3 to work when running a service. I did try just using backticks and that appears to work, see readmore... for some small modifications.

use strict; use IPC::Run3; # Exports run3() by default use Win32::Daemon; use Cwd 'abs_path'; my $script = abs_path($0); my $logfile = $script; $logfile =~ s/\.[^\.\\\/]*$/.log/; my $svcname = $script; $svcname =~ s/^.*[\/\\]//g; $svcname =~ s/\.[^\.]*$//; print "service name: $svcname\n"; print "Log file: $logfile\n"; if($ARGV[0] eq 'install') { my %service_info = ( machine => '', name => $svcname, display => $svcname, path => $^X, user => '', pwd => '', description => 'test case', parameters => $script ); if( Win32::Daemon::CreateService( \%service_info ) ) { print "Successfully added.\n"; } else { print "Failed to add service: " . Win32::FormatMessage( Win32: +:Daemon::GetLastError() ) . "\n"; } exit; } Win32::Daemon::StartService(); open(LOG,'>>',$logfile) or die("Failed to write to logfile\n$!\n"); Win32::Daemon::State( Win32::Daemon->SERVICE_RUNNING ); print LOG "Running this service...\n"; print LOG localtime()."\n"; #run3(['ping.exe', '-n', '5', '127.0.0.1'],\undef,\*LOG); my @result = `ping -n 5 127.0.0.1`; ########### changed print LOG @result; ########### changed my $ec = $? >> 8; print LOG "Exit Code $ec\n\n"; close(LOG); print "Exit Code $ec\n"; Win32::Daemon::StopService();

In reply to Re: Win32::Daemon + IPC::Run redirect problem by Marshall
in thread Win32::Daemon + IPC::Run redirect problem by chris212

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.