Santhosh
Santhosh

Reputation: 6707

How do I exit the command shell after it invokes a Perl script?

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.

Upvotes: 0

Views: 16436

Answers (5)

catwalk
catwalk

Reputation: 6476

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.

Upvotes: 0

Pete Duncanson
Pete Duncanson

Reputation: 3246

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?

Upvotes: 3

Frank Bollack
Frank Bollack

Reputation: 25166

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).

Upvotes: 5

Andomar
Andomar

Reputation: 238076

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.)

Upvotes: 3

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23178

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

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

Upvotes: 4

Related Questions