zejames has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow monks, I've got a little problem when trying to use Win32::Daemon module...

My goal : I want to create a "daemon" for a win32 system. The only way I found (not a windows expert though...) is to make my script a service, thanks to Win32::Daemon.

So I created the following test script (from the Win32::Daemon first example) :

use Win32::Daemon; # Tell the OS to start processing the service... Win32::Daemon::StartService(); print "Waiting...\n"; # Wait until the service manager is ready for us to continue... print "SERVICE_NOT_READY " . SERVICE_NOT_READY . "\n"; print "SERVICE_STOPPED " . SERVICE_STOPPED . "\n"; print "SERVICE_RUNNING " . SERVICE_RUNNING . "\n"; print "SERVICE_PAUSED " . SERVICE_PAUSED . "\n"; print "SERVICE_START_PENDING " . SERVICE_START_PENDING . "\n"; print "SERVICE_STOP_PENDING " . SERVICE_STOP_PENDING . "\n"; print "SERVICE_CONTINUE_PENDING " . SERVICE_CONTINUE_PENDING . "\n"; print "SERVICE_PAUSE_PENDING " . SERVICE_PAUSE_PENDING . "\n"; while( SERVICE_START_PENDING != Win32::Daemon::State() ) { sleep( 1 ); print Win32::Daemon::State() . "\n"; } print "Running...\n"; # Now let the service manager know that we are running... Win32::Daemon::State( SERVICE_RUNNING ); # Okay, go ahead and process stuff... unlink( glob( "c:\\temp\\*.tmp" ) ); # Tell the OS that the service is terminating... Win32::Daemon::StopService();
and I have this :
Waiting... SERVICE_NOT_READY 0 SERVICE_STOPPED 1 SERVICE_RUNNING 4 SERVICE_PAUSED 7 SERVICE_START_PENDING 2 SERVICE_STOP_PENDING 3 SERVICE_CONTINUE_PENDING 5 SERVICE_PAUSE_PENDING 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 Terminating on signal SIGINT(2)

So it never reaches the SERVICE_RUNNING state : it passes from SERVICE_NOT_READY to SERVICE_STOPPED...
Any idea on the problem ? I run Windows XP SP1, with ActivePerl 5.8.4.

Many thanks


--
zejames

20040915 Edit by castaway: Changed title from 'Problems with Win32::Daemon'

Replies are listed 'Best First'.
Re: Win32::Daemon service doesn't reach RUNNING state
by BrowserUk (Patriarch) on Sep 14, 2004 at 11:17 UTC

    Services need to be installed, not just run.

    Take a (another) look at theWin32::Daemon page. In particular, the more complete example of a service at Example 2 and the installer script at Example 3.

    Alternatively, you may find Jenda's Win32::Daemon::Simple easier to use.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Ok.

      As I said, I just wanted to create a simple daemon, with a few lines of code as possible. But it seems that few under Windows is quite much ;)

      Kind regards

      --
      zejames

        Explain what you mean by "a daemon"?

        If you just mean a background task, that's a lot easier than creating a service--which has the daemon-like properties of running detached but is an aweful lot more besides.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Win32::Daemon service doesn't reach RUNNING state
by SwaJime (Scribe) on May 23, 2019 at 14:09 UTC
    Hi,

    I have the same symptoms as above.

    I have a full-fledged service based on Win32::Daemon.

    It is working correctly on at least 5 computers.

    On the remaining computers, the section in the if statement shown below never executes.

    I have modeled after example 5 on https://metacpan.org/pod/Win32%3A%3ADaemon

    sub Callback_Running { my($Event, $Context) = @_; print "State: " . Win32::Daemon::State() . "\n"; if(SERVICE_RUNNING == Win32::Daemon::State()) { Log 1, "DBSAgent is checking in.";;

    On the server I can troubleshoot on, State displayed as 1: SERVICE_STOPPED on the first call, then 0: SERVICE_NOT_READY on each call after that. It never displayed as 4: SERVICE_RUNNING like it was supposed to.

    The server I am troubleshooting on is Windows Server 2012 R2 Standard 64-bit v6.3.9600

    Help is greatly appreciated! Thanks :-)

      Note that the thread you're replying to is 15 years old. I would recommend starting a new thread, and including a minimal but runnable SSCCE that reproduces the issue on your end.