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

Hi Everyone, I'm new to Perl, please bare with me. Am trying to write a perl program that checks to see if a certain process is running, if the process is "not" running I want to start it. This piece am able to do. The issue is that the command has to run inside of a Dos, so when the command is executed the command windows has to be left open. Is there a way to do this with Perl? if so, could someone please let me know?

Thanks!

Replies are listed 'Best First'.
Re: Executing a Dos command
by wfsp (Abbot) on Aug 11, 2007 at 06:01 UTC
    You double click on the perl script, a window opens, the results flash by, the window closes?

    You want the window to stay open so you can read the output?

    If that is the case something like this will do it.

    #!/usr/bin/perl use strict; use warnings; # do something print "results\n"; # do something else print "more results\n"; system('dir'); print "done. hit return\n"; <>;
    The <> waits for input from STDIN (the keyboard in this case) and will prevent the window closing.

      You've got to be careful with <>, because it will also process command line arguments, which can lead to unexpected errors.

      C:\>perl -e "<>;" -- foo Can't open foo: No such file or directory at -e line 1.

      If a file named 'foo' actually exists, then a line would will be read from it, and the script will disappear as if this step was never taken.

      I usually use <STDIN>; or if I am reliving my old habit of batch scripting, `pause`;. The pause approach will continue after any key press, not just 'ENTER', but it does spawn a shell. Choose your poison.


      TGI says moo

Re: Executing a Dos command
by dogz007 (Scribe) on Aug 11, 2007 at 01:28 UTC
    Although you'd like to run a DOS command, I'd be willing to bet you're running a windows machine. I've had trouble running exec on ActiveState Perl before, so I usually use system instead. For instance, if I wanted to open an XML file that I just finished writing so that I can view the results, I've written in the past:

    # open file for output open my $file, ">${title}.xml" or die "Couldn't open file: $title.xml +\n\n"; select $file; ## Print lots of XML data here # open GGobi for viewing close $file; system "ggobi.exe ${title}.xml";

    In addition, if I use system, my Perl scripts waits around until the new process is terminated.

Re: Executing a Dos command
by Ash Rai (Acolyte) on Aug 10, 2007 at 21:24 UTC
    You may be looking for this:
    exec
Re: Executing a Dos command
by BrowserUk (Patriarch) on Aug 11, 2007 at 23:32 UTC

    Your question is very ambiguous, but my interpretation of what you are asking for is to be able to start a new command line program from within your perl script asynchronously. If that is the case, then something like this would work.

    system 'start cmd /k theProgram arg1 arg2';

    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.
Re: Executing a Dos command
by jcc2007 (Initiate) on Aug 12, 2007 at 02:05 UTC
    First of all, thanks for the replies!
    Sorry for not being more clearer on the question. I would like to get a little more clarification, when I use for example the system(), will this allow the program to stay up and running?
    basically what I want this program to do is the following:
    1. Create a schedule task that runs every hour.
    2. Check the process by using ntprocinfo.
    3. If the process is not on the list to start it.
    When I execute the program manually from the DOS Shell, basically what it does it connects to a database and sends the data to the database and stays listing. Another program generates the data and goes thru the same cycle.
    Thanks in advance.