PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! Basic question: How do I close a command prompt under windows 10? The basic exit; does not work under windows 10. I've checked my scripts under windows xp, vista, 7, & 8 and it works just fine. Am I missing something for it to work under windows 10? The sound plays and the script does end...however it just goes to the command line and does not close the prompt. Thanks in advance!
sub quit { Title "Exit | You Are Now Leaving"; sleep 2; my $byeresult = Win32::Sound::Play("SystemExit"); exit; } }

Replies are listed 'Best First'.
Re: Windows 10 Command Prompt
by NetWallah (Canon) on Apr 28, 2020 at 20:29 UTC
    This is crude and brutal, but it works for me:
    perl -e "for (qx[wmic process get processid,parentprocessid,executable +path ]){next unless m/perl.+\Q$$\E/;$parent=(split /\s+/,$_) [1] ;qx| +taskkill /PID $parent|} "
    It kills the command prompt where this code is called from.

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

      why not just Win32::Getppid (oops thanks soonix!)?

      use Win32::Getppid; my $parent_pid = getppid;

      bw, bliako

      edit: primitive os, brutal practices :)

        @bliako: Thanks this worked to get the process ID!

      @NetWallah: yes and it does work as expected.

Re: Windows 10 Command Prompt
by Discipulus (Canon) on Apr 29, 2020 at 12:56 UTC
    Hello PilotinControl

    I see no differences in win7 win10 behaviour for the following simple script (removing sounds and other amenities)

    use strict; use warnings; $|++; #autoflush output quit(); sub quit { my $sleep = 3; while ( $sleep ){ print "\rQuitting in $sleep sec."; sleep 1; $sleep--; } exit; }

    I find no differences even if the .pl program is doubleclicked AND .pl files are associated with a valid perl.exe interpreter. Is this your use case? Because normally a command prompt previously started is persistent. Anyway in both circumstances and in both OSs I see no different behaviour.

    You can be interested in a more complex solution as you can read in Handling MSWin Script Output, where I posted the following code:

    use strict; use Win32::Process::Info; my $pihandle = Win32::Process::Info->new(); my @procinfo = $pihandle->GetProcInfo(); my $ParentPID; my %ProcNames; foreach my $PIDInfo (@procinfo) { $ProcNames{$PIDInfo->{ProcessId}} = $PIDInfo->{Name}; if ($PIDInfo->{ProcessId} == $$) { $ParentPID = $PIDInfo->{ParentProcessId}; last; } } print "Parent's name is [", $ProcNames{$ParentPID}, "]\n"; if ( $ProcNames{$ParentPID} eq 'OpenWith.exe' or $ProcNames{$ParentPID +} eq 'explorer.exe') { print 'Press ENTER to close the window: '; <>; } else{ print "..exiting normally\n"; }

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Windows 10 Command Prompt
by Marek1415 (Acolyte) on Apr 28, 2020 at 20:39 UTC

    Actually exit exits perl program and returns value, it doesn't close terminals. It looks as if you opened cmd then ran perl program which will kill cmd. You can run program using perl interpreter directly which will close terminal immediately after finishing script. You can also add something like this but this will kill all command prompts.

    system("taskkill /im cmd.exe /f");

      This is at best unwise, this will silently forcibly kill all instances of cmd.exe, you have no idea what the user may be doing on their system.

      This was tested and does not work under Windows 10.

Re: Windows 10 Command Prompt
by Anonymous Monk on Apr 29, 2020 at 13:44 UTC
    Just run a shell command which invokes Perl then does "exit." No point in calisthenics to "do it all in Perl."