Win32::Detached - daemonize perl scripts under windows, without a console window
version 1.103080
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
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.
Returns the default command line argument string which signals that the script should run normally and not detach.
Checks the command line and certain system variables to see whether we want to detach or not. Cleans ARGV and returns if not.
Builds the command line for the detached clone, executes it to start the clone and then exits itself.
Christian Walde <mithaldu@yahoo.de>
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
To install Win32::Detached, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Win32::Detached
CPAN shell
perl -MCPAN -e shell install Win32::Detached
For more information on module installation, please visit the detailed CPAN module installation guide.