Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a command forever in Powershell?

Tags:

powershell

I'm running a command that I want to rerun when it completes, without needing to navigate back to the terminal to enter the command again.

I know in Ubuntu, I can run a terminal with a command, and it'll loop forever if I have it set up right, something like gnome-terminal -x $MY_COMMAND.

Given that I can't mark Powershell to rerun the command instead of closing the window, how can I repeat a command indefinitely?

like image 796
TankorSmash Avatar asked Sep 12 '17 17:09

TankorSmash


2 Answers

Turns out the answer is fairly straight forward, wrap the command in a loop forever. This'll allow you to Ctrl-C out of it, and it'll keep repeating even after your command completes or otherwise exits the first time.

while ($true) {
  my_command;
}

Or in my case as a one liner: while ($true) { python3 .\manage.py runserver_plus 8080; }

like image 99
TankorSmash Avatar answered Oct 01 '22 03:10

TankorSmash


This will infinitely run a command repeatedly after execution completes:

While (1)
{
    Start-Process -FilePath 'python3' -ArgumentList @('.\manage.py','runserver_plus','8080') -Wait -ErrorAction 'SilentlyContinue'
}

Alternatively:

#requires -Version 3
While (1)
{
    Try {
        $Params = @{FilePath='python3'
                    ArgumentList=@('.\manage.py','runserver_plus','8080')
                    Wait=$True
                    ErrorAction='Stop'}
        Start-Process @Params
    } Catch {
        <# Deal with errors #>
        Continue
    }
}
like image 21
Maximilian Burszley Avatar answered Oct 01 '22 04:10

Maximilian Burszley