Your skill will accomplishwhat the force of many cannot PerlMonks

### starting a process in the background, with a twist

 on Mar 20, 2012 at 19:45 UTC ( #960639=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

if I were to ask "how do I start a process in the background then kill it after a pre-defined amount of time?", you would direct me to the FAQs, and rightly so.

However, there are two complicating factors: I need to do this on Windows, and I can't use any modules from CPAN, only those that come with one of the common Windows-oriented Perl distributions.

The reason for the second requirement is that I have to work in a very restricted environment where access to the internet is not allowed.

What I actually need this for: I want to write an automated testing tool for one of the programs that I'm working on. This is a huge application whose behavior can be modified with hundreds of options scattered around dozens of config files, and I suspect that there exists a winning combination of options that enable a certain functionality that so far I couldn't entice from the program. Manual testing of all possible combination of all the suspected combination of options would be tedious and error prone, hence the need for this script.

Comment on starting a process in the background, with a twist
Re: starting a process in the background, with a twist
by BrowserUk (Pope) on Mar 20, 2012 at 20:24 UTC
I can't use any modules from CPAN, only those that come with one of the common Windows-oriented Perl distributions.

That's a bit like asking: "How can I get to the top of teh building when I've a broken leg and refuze to use the lift?"

I have to work in a very restricted environment where access to the internet is not allowed.

Can you carry in a flash drive? Or a CD/DVD? A floppy disk? At a pinch you could print the module(s) you need to paper at home, carry the paper in and type them in at work.

"how do I start a process in the background then kill it after a pre-defined amount of time?"

Ostensibly all you need is:


...

my $pid = system 1, q[ c:\the\path\to\theProgram.exe /option1 /option2 + ]; sleep$timeout;

kill 2, \$pid;

...
[download]

But there are always complications. You'll want to capture the output; supply "user input'; click on pop-ups; and do it from a cgi script running in a VM hosted on a remote server in Timbuktu.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Yes, I can download the tar.gzipped modules from CPAN and install them manually, but looking at the possibly relevant modules in the FAQ (Proc::Background, Win32::Process etc.) I saw that each of them has non-trivial dependencies.

Anyway, your suggestion works (except that I needed kill 9 instead of kill 2). You are right that there are complications, for example you were right about the VM, there is one involved in the problem :), but for this specific case, it looks like that your solution will be adequate.

Thanks!

PS. I propose that this method (system 1, "program_name") be added to the relevant entry in perlfaq8, because it's simple and useful. A similar hack for Unix-like systems is already present there.

PS. I propose that this method (system 1, "program_name") be added to the relevant entry in perlfaq8, because it's simple and useful. A similar hack for Unix-like systems is already present there.

You could propose it to the Perl5 Porters via their mailing list by submitting a patch as described in perlhack#SUPER-QUICK-PATCH-GUIDE.

Dave

Maybe Win32::Spawn in Win32?
Re: starting a process in the background, with a twist
by Happy-the-monk (Monsignor) on Mar 21, 2012 at 10:42 UTC

then kill it after a pre-defined amount of time

Alternatively you might check time() inside your process and finish it off gracefully by its own controls when it has aged that pre-defined amount of time, saving you the hassle of using signals from outside.

Cheers, Sören

Create A New User
Node Status?
node history
Node Type: perlquestion [id://960639]
Approved by Eliya
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2015-03-29 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?