Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Delete files every 5 sec. Register as Win Service

by rockets12345 (Novice)
on Feb 23, 2005 at 23:56 UTC ( [id://433908]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to perl and am still learning can someone help me how to write a perl script that loops through a particular directory every five seconds and deletes any files in it. And then register this script as windows service. Thanks
  • Comment on Delete files every 5 sec. Register as Win Service

Replies are listed 'Best First'.
Re: Delete files every 5 sec. Register as Win Service
by esskar (Deacon) on Feb 24, 2005 at 00:29 UTC
    use strict; use Win32::Daemon; # Tell the OS to start processing the service... Win32::Daemon::StartService(); # Wait until the service manager is ready for us to continue... while(SERVICE_START_PENDING != Win32::Daemon::State()) { sleep(1); } # Now let the service manager know that we are running... Win32::Daemon::State( SERVICE_RUNNING ); # Okay, go ahead and process stuff... while(1) { # delete all *.tmp files at c:\temp unlink( glob( "c:\\temp\\*.tmp" ) ); last unless keep_alive(); sleep(5); # and wait last unless keep_alive(); } # Tell the OS that the service is terminating... Win32::Daemon::StopService(); sub keep_alive { my $msg = Wun32::Daemon::QueryLastMessage(); return ( $msg != SERVICE_STOP_PENDING and $msg != SERVICE_CONTROL_SHUTDOWN ); }
    Update1:
    look at Win32::Daemon to see how to install the service
    Update2:
    Added the $sunning and QueryLastMessage part
    Update3:
    replaced Update2 by sub keep_alive
Re: Delete files every 5 sec. Register as Win Service
by FitTrend (Pilgrim) on Feb 24, 2005 at 01:54 UTC

    The only other thing to note is if you are going to have it run as a service, make sure the service has proper permissions to delete files from the hard drive. LocalSystem may not cut it.

Re: Delete files every 5 sec. Register as Win Service
by Jenda (Abbot) on Feb 25, 2005 at 21:08 UTC
    use strict; use warnings; no warnings 'uninitialized'; use Win32::Daemon::Simple Service => 'DelService', Name => 'SERVICE NAME', Version => 'x.x', Info => { display => 'SERVICEDISPLAYNAME', description => 'SERVICEDESCRIPTION', # user => '', # pwd => '', interactive => 0, }, Params => { # the default parameters Interval => 5*(1/60), # it's in minutes! LogFile => "ServiceName.log", Directory => 'c:\the\directory\\', }; ServiceLoop(\&doTheJob); Log("Going down"); exit; sub doTheJob { opendir my $DIR, DIRECTORY or die qq{Can't read the contents of "} . DIRECTORY . qq{" !\n +}; while (my $file = readdir $DIR) { next if -d (DIRECTORY . '/' . $file); # don't try to unlink di +rectories # you can add some filtering here if you do not want to delete + everything unlink (DIRECTORY . '/' . $file); } closedir $DIR; }

    This is a complete program! Able to install and deinstall itself as a service, allowing you to change the parameters stored in the Windows registry from the command prompt, etc. etc. Check out Win32::Daemon::Simple on CPAN or on my page.

    HTH, Jenda
    We'd like to help you learn to help yourself
    Look around you, all you see are sympathetic eyes
    Stroll around the grounds until you feel at home
       -- P. Simon in Mrs. Robinson

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://433908]
Approved by johnnywang
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found