简体   繁体   中英

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.

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

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

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

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM