A union of curiosity and data science

Knowledgebase and brain dump of a database engineer


Powershell script to restart Ollama on crash, close or frozen

 

The script below checks for ollama service on the standard localhost port. If it can't be reached, we kill the process and start a new one. 

The environment variables allow for hosting on your local network, multiple models loaded (3), parallel execution of request (2) and storage of models in gpu memory for 1 hour of non usage before being released.  

 

# Path to the Ollama server executable
$ollamaPath = "C:\<path to ollama>\ollama.exe"

# Function to check if the Ollama server is running
function Check-OllamaServer {
    try {
        # Send a request to the Ollama server
        $response = Invoke-WebRequest -Uri "http://localhost:11434" -TimeoutSec 10 -UseBasicParsing
        return $true
    } catch {
        return $false
    }
}

# Function to restart the Ollama server
function Restart-OllamaServer {
    # Define the environment variables
    $env:OLLAMA_HOST = "0.0.0.0"
    $env:OLLAMA_NUM_PARALLEL = 2
    $env:OLLAMA_MAX_LOADED_MODELS = 3
    $env:OLLAMA_KEEP_ALIVE = "60m"

    # Kill existing Ollama processes
    Get-Process -Name "ollama" -ErrorAction SilentlyContinue | Stop-Process -Force

    # execute the command
    Start-Process -FilePath $ollamaPath -ArgumentList "serve"

}


# Main loop to check the server health every 10 seconds
while ($true) {
    if (-not (Check-OllamaServer)) {
        Write-Output "Ollama server is not responding. Restarting..."
        Restart-OllamaServer
    } else {
        Write-Output "Ollama server is running normally."
    }

    # Wait for 1 minute before checking again
    Start-Sleep -Seconds 10
}