Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

While doing force quiting the exe how to run a bat file

by sudhakar1k (Initiate)
on Feb 26, 2010 at 07:45 UTC ( #825457=perlquestion: print w/replies, xml ) Need Help??
sudhakar1k has asked for the wisdom of the Perl Monks concerning the following question:


Can any one help me???

If the end user quit the exe(force quiet) i need to run a bat file. Is their any option in perl?

Thanks for looking into this.

  • Comment on While doing force quiting the exe how to run a bat file

Replies are listed 'Best First'.
Re: While doing force quiting the exe how to run a bat file
by Corion (Pope) on Feb 26, 2010 at 07:54 UTC

    Look at END blocks or %SIG (in perlvar):

    END { print "End of program was reached.\n"; };

      Dear Corion

      Thanks for the method, but its not working can you please help me. End of the coding i have placed.

      Thank you

        "It's not working" does not help me diagnose the error/situation you have. Please post a short, self-contained example, and describe what you are doing, and what the outcome is. Also describe what you would like to happen instead.

Re: While doing force quiting the exe how to run a bat file
by cdarke (Prior) on Feb 26, 2010 at 09:19 UTC
    Could you clarify what you mean by "force quiet" (did you mean "force quit"?), and how exactly that is being done to a Perl program?

    Since you say you want to run a bat file then I guess you are running on Windows (unless its something you keep in the bat cave), but please confirm the OS - that can be significant, particularly when suggesting solutions involving signals.

    Some Microsoft Windows actions call the TerminateProcess API, and that will just kill the application stone dead without any chance of recovery or tidyup, so it could be that there is no option but to tell the users to stop doing it. Finding out why they are killing the process might be a good move.
Re: While doing force quiting the exe how to run a bat file
by Marshall (Abbot) on Feb 28, 2010 at 04:35 UTC
    I'm not even sure that this is a Perl question. I also think some more specifics would help, but the following may help you...

    When a program exits, a return code is made available to the Windows shell. The normal code for "everything worked fine" is "0", zero. Non-zero codes are typically used for error conditions.

    My thinking would be that since you want to run a Windows .bat file if this .exe is aborted by CTL-C, i.e. QUIT, then I would start this .exe from a Windows .bat file and check the return value to the shell and then run your "error .bat file" from this "starter" .bat file!

    Below I wrote a very simple C program and show a simple .bat file to illustrate how to do this. The C program just echos input lines until you enter "quit". If you enter "quit" the program returns "0" to the shell. However, if you "abort", i.e. force the program to "quit" with CTL-C, it will return a code of "512" to the shell.

    So basically, I can tell if the program stopped because of user entering "quit" versus program stopped because of CTL-C.

    Type "help start" at your Windows command line for more options on the START command. The /WAIT option is important here! Also I just printed the ERRORLEVEL, I don't know exactly what you have in mind for your "fix-it" .bat file. You will need an IF statement on %ERRORLEVEL%.

    *** test.bat *** START /wait echoline ECHO %errorlevel% ****echoline.C **** #include <stdio.h> int main(void) /*simple test program: echoline.C */ { char line[256]; while (printf("%s","Enter Line:"),gets (line) && strcmp(line,"quit") != 0) { printf ("%s\n",line); } }
    Oh another point, ERRORLEVEL is NOT an environment variable! ERRORLEVEL is not %ERRORLEVEL%

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://825457]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-05-27 16:38 GMT
Find Nodes?
    Voting Booth?