The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Win32::Detached - daemonize perl scripts under windows, without a console window

VERSION

version 1.103080

SYNOPSIS

    use strict;
    use warnings;

    use Win32::Detached; # at this point the script will background itself
                         # if started via Win->Run or double-clicking,
                         # the console window will close

    sleep 10; # so you can see the process in the task manager

    print "moo"; # this will not show up anywhere

On the command line:

    # to daemonize:
    script.pl

    # to run normally
    script.pl --no_detach

DESCRIPTION

First off, I am not the one who originally wrote this. I found it years ago on Perlmonks, only remembered it now and figuring it was time to get it out.

This module allows you to daemonize any perl script under windows. This may be useful to have a service/daemon in the background, or when running a desktop application and being annoyed by the console window.

When the module is loaded it inspects @ARGV for presence of "--no_detach" If it finds it, it removes it from @ARGV and then returns, doing nothing else.

If that flag is not present however it relaunches the script that loaded it with the same arguments (plus --no_detach), in the same working directory. Then it exits the current script. You can think of it as cloning itself and then comitting suicide.

Take note: You will want to use this module before any other module that actually executes code.

If you don't like the name of the skip flag, you can override the sub Win32::Detached::_skip_flag however you like. Easiest is probably Sub::Exporter and its "into" facility.

SUBROUTINES

skip_flag

Returns the default command line argument string which signals that the script should run normally and not detach.

check_args

Checks the command line and certain system variables to see whether we want to detach or not. Cleans ARGV and returns if not.

detach

Builds the command line for the detached clone, executes it to start the clone and then exits itself.

AUTHOR

Christian Walde <mithaldu@yahoo.de>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2010 by Christian Walde.

This is free software, licensed under:

  DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 2004