0

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.

4
  • In bash, you'd invoke the perl script with "exec foo.pl". Commented Jan 12, 2010 at 13:47
  • Sorry if i forgot to mention, i am running it in Windows :-) Commented Jan 12, 2010 at 13:51
  • This may be difficult to accomplish; the key is to send a SIGHUP signal to the parent process, but at least in ActiveState Perl, getppid() is unimplemented. Commented Jan 12, 2010 at 13:55
  • Looks like we have the following approaches : [1] If i am opening the command shell only for running the perl script, then i can do cmd.exe /C perl myscript.pl or start.exe /B perl myscript.pl [2] If i am not already in any command shell and i just want to invoke the perl script, then i can write a batch file which will invoke the script and then call exit. like : perl myscript.pl; exit; Thanks!! Commented Jan 12, 2010 at 14:32

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Noufal: In UNIX shells, the command separator is ; not & (which backgrounds a job).
4

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

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

2 Comments

For that matter, you can just run perl yourscript.pl and leave cmd.exe out of it entirely.
On Windows 10, if I start Command Prompt, and only type perl - which will run nothing, how do I close the Command Prompt window that is still open? Tried quit bye exit all to no avail.
3

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

Comments

3

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?

Comments

0

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.