Home > Net >  Run octave script from powershell and wait for it to finish
Run octave script from powershell and wait for it to finish

Time:04-29

I'm trying to run a very simple octave .m file from Powershell but i can't manage to make powershell wait for it to finish. In fact, it launches the script execution but then immediatly starts executing the next line.

As I said the script is really simple, just a test

a=100
% Saving just to be sure that the script excuted
save test.mat a

% Pausing to be sure that the execution is not too fast  
pause(10)
disp("no way")

And in powershell I simply run

octave --persist test.m

but prompt doesn't wait for octave to finish execution. It seems somehow it runs it async in another process.

I've tried running the script from batch with the wait option

START /W octave --persist test.m

but the result still the same.

Thanks in advance

EDIT 1

Thanks to @Trey Nuckolls I'm using this patch:

$Donefilename = 'done'
if (Test-Path $Donefilename) {
    Remove-Item $Donefilename
    Write-Host "Last execution $Donefilename has been deleted"
  }
else {
    Write-Host "$Donefilename doesn't exist"
}

octave --persist test.m

do{
    $i  
    Start-Sleep -Seconds 2 
    $OctaveComplete = Test-Path -Path $Donefilename 
 }
 until ($OctaveComplete -or ($i -eq 30))

Making the octave script writing an empty "done" file at the end of execution. Not the best solution although; I'm not able to redirect the execution output for example.

EDIT 2

So, i managed to find the problem thanks to all your responses and comments. It seems that when i was calling octave from windows it wasn't calling the executable but something else. Getting the right path and executing:

& "C:/Program Files/GNU Octave/Octave-6.4.0/mingw64/bin/octave-cli.exe" test.m 

works perfectly (you need just to add exit at the end of the script). So, it was a matter of path.

CodePudding user response:

You might consider putting a wait loop into your invoking script like...

do{
   $i  
   Start-Sleep -Seconds 10 #Assuming that 30 seconds is way too long
   $OctiveComplete = <**Boolean returning function that becomes 'True' when run is complete**>
}
until ($OctiveComplete -or ($i -eq 4))

This would go directly after the line that invokes Octave.

  • Related