tags:

views:

440

answers:

5

If I run a Perl script from a command prompt (c:\windows\system32\cmd.exe), how can I exit the command prompt after the script finishes executing.

I tried system("exit 0") inside the Perl script but that doesn't exit the cmd prompt shell from where the Perl script is running.

I also tried exit; command in the Perl script, but that doesn't work either.

+3  A: 

You can start the program in a new window using the START Dos command. If you call that with /B then no additional window is created. Then you can call EXIT to close the current window.

Would that do the trick?

Pete Duncanson
+4  A: 

Have you tried cmd.exe /C perl yourscript.pl ?

According to cmd.exe /? /C carries out the command specified by string and then terminates.

Arkaitz Jimenez
For that matter, you can just run `perl yourscript.pl` and leave `cmd.exe` out of it entirely.
Rob Kennedy
A: 

You can send a signal to the parent shell from Perl:

 kill(9,$PARENT_PID);`

Unfortunately, the getppid() function is not implemented in Perl on windows so you'll have to find out the parent shell PID via some other means. Also, signal #9 might not be the best choice.

catwalk
Do you even have a SIGKILL (signal no. 9) on windows?
Noufal Ibrahim
@Noufal: I am sure SIGKILL (as sent from perl) does it job on windows; not sure how it is implemented though
catwalk
+5  A: 

Try to run the Perl script with a command line like this:

perl script.pl & exit

The ampersand will start the second command after the first one has finished. You can also use && to execute the second command only if the first succeeded (error code is 0).

Frank Bollack
This I believe is UNIX specific and won't work with windows.
Noufal Ibrahim
No it is not, look here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
Frank Bollack
+1 Very interesting
Andomar
ephemient
+3  A: 

If you're starting the command shell just to run the perl script, the answer by Arkaitz Jimenez should work (I voted for it.)

If not, you can create a batch file like runmyscript.bat, with content:

@echo off
perl myscript.pl
exit

The exit will end the shell session (and as a side effect, end the batch script itself.)

Andomar