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

I expect this to be a simple question, but the answer is not obvious to me atm. Basically, on my windows machine, I run qbittorrent and it sometimes needs to be restarted. The script I wrote works but it doesn't exit.
#!/usr/bin/env perl #use 5.36.1; use strict; use warnings; my $Software_Path = "C:/Program Files/qBittorrent/qbittorrent.exe"; my $Wait_Time = 10; # Implement Time Delay system qq[taskkill /im qbittorrent.exe]; sleep int(rand($Wait_Time)); system qq[$Software_Path]; exit

Replies are listed 'Best First'.
Re: how to system call that exits
by BillKSmith (Monsignor) on Apr 27, 2024 at 19:01 UTC
    Your program does exit after qbittorrent exits. Replace your last system with exec. It will terminate your program if it succeeds, otherwise, it will return and execute your exit. In either case your program terminates.
    Bill
      I had tried exec earlier. My obstacle was executing a windows program which has spaces in it's file path, i.e., "program files". I've tried it with double quotes, qq[], with and without the (), but it is always halting on 'C:/Program' is not recognized as an internal or external command....
      The only solution was to add the program file path to the Environmental variables PATH. I guess exec doesn't work as well as system on Windows OSs.
      exec ("qbittorrent.exe") or print STDERR "couldn't exec qbittorrent: $ +!";
        You might consider using Proc::Background, which specializes in running other processes platform-independently (with emphasis on windows), and gives you an object to monitor and kill it. Then you can also write the rest of the script to periodically check for the case of when bittorrent needs restarted, and not be blocked by waiting for system().

        You can also properly quote your filename for cmd.exe and then it will work as well:

        my $cmd = "C:\\Program Files\\qbittorrent\\qbittorrent.exe"; my @args = (...); my @cmd = (qq{"$cmd"}, @args); system @cmd == 0 or warn "Couldn't launch [@cmd]: $! / $^E";
        As Corion pointed out below, with proper quoting, both functions work the same. I find it surprising that system does work as expected without the extra quotes. I suspect that it is a difference between cmd and Windows API.
        Bill
Re: how to system call that exits
by Anonymous Monk on Apr 28, 2024 at 07:05 UTC
    Check your system calls for errors:
    system(qq[$Software_Path]) == 0 or die "system failed: $?";
Re: how to system call that exits
by harangzsolt33 (Deacon) on Apr 28, 2024 at 02:17 UTC
    Put the word "START" in front of C:\Program...