Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
more useful options
 
PerlMonks  

To run win command with perl with no wait for process to complete and perl to continue furher

by Selva123 (Novice)
on Nov 08, 2011 at 08:39 UTC ( #936683=perlquestion: print w/ replies, xml ) Need Help??
Selva123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have to execute a windows command with perl and my requirement is just to run the Command and no need to bother the status of the same which I will track with the same perl script after some time.

Hence I do not need system() and backtick commands which will wait for the process to run and complete. When I use exec(), it is terminating perl. What is the option to do the same? I heard that exec() with fork and waitpid() may handle the same but I am totally unaware of the same. Will someone help me with code snippet of the same with notepad.exe as example? or some other perl command to cater my purpose.

Comment on To run win command with perl with no wait for process to complete and perl to continue furher
Re: To run win command with perl with no wait for process to complete and perl to continue furher
by Anonymous Monk on Nov 08, 2011 at 08:46 UTC
      why not just use system("$cmd &") ? you can make that safer by calling it like so: system("/bin/sh", "-c", "$cmd &").
        ignore my suggestion- skipped over the windows requirement
      Thanks and this is working and I could able to track the same later with Alive and die to kill after some time it exists.

      Here I have a question. Do you have any equivalent to work on perl in win 64 bit box?

Re: To run win command with perl with no wait for process to complete and perl to continue furher
by DrHyde (Prior) on Nov 08, 2011 at 10:42 UTC

    You need to fork() first, then exec(). Windows doesn't have fork(), but IIRC perl on Windows fakes it up somehow.

    I expect that there's also a Win32::SomethingOrOther that would do the same thing.

Re: To run win command with perl with no wait for process to complete and perl to continue furher
by dasgar (Deacon) on Nov 09, 2011 at 06:01 UTC

    A non-perl solution would be to take advantage of the "start" command that is available from the Windows command prompt. This command opens a new command prompt, runs a command in that new command prompt, and will by default immediately return control back to the original command prompt.

    For example, if you open a command prompt and run the following:

    start "test" dir c:

    You should see a new command prompt get opened with the title of "test" and in the command prompt the command "dir c:" was run. Also, the original command prompt does not wait for the newly created command prompt before going back to waiting for user input.

    So if you use the combination of system() (or back ticks) along with the "start" command, you should be able to accomplish what you want. Might not be the "best" way to do things, but should still enable you to issue command prompt commands from Perl and not wait for that command to finish before your code continues on.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://936683]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-04-19 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (475 votes), past polls