Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash· GoogleHugging Face LMArena Human Preference 140K

mac CLI. how can I start ollama server via script without a user on the terminal? I…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwq-32b’s answer.
  1. User

    mac CLI. how can I start ollama server via script without a user on the terminal? I don't want to use launchctl

  2. Gemini Assistant

    You want to start the Ollama server in the background on macOS without it being tied to a terminal session, and without using launchctl. This is a common pattern for daemonizing simple processes.

    The key is to use nohup and background the process (&), along with managing its Process ID (PID) so you can stop it later.

    Here's a bash script that provides start, stop, and status functionalities:

    #!/bin/bash
    
    # Configuration
    OLLAMA_PATH="/usr/local/bin/ollama" # Adjust if your ollama executable is elsewhere
    PID_FILE="$HOME/.ollama/ollama.pid"
    LOG_FILE="$HOME/.ollama/ollama.log"
    
    # Ensure the .ollama directory exists for pid and log files
    mkdir -p "$(dirname "$PID_FILE")"
    
    # --- Functions ---
    
    is_running() {
        if [ -f "$PID_FILE" ]; then
            PID=$(cat "$PID_FILE")
            # Check if a process with this PID is running and its command matches "ollama serve"
            if ps -p "$PID" -o command= | grep -q "ollama serve"; then
                return 0 # Running
            fi
        fi
        return 1 # Not running
    }
    
    start_ollama() {
        if is_running; then
            echo "Ollama server is already running (PID: $(cat "$PID_FILE"))."
            return 0
        fi
    
        echo "Starting Ollama server..."
        # nohup runs the command immune to hangups
        # > "$LOG_FILE" 2>&1 redirects both stdout and stderr to the log file
        # & puts the process in the background
        nohup "$OLLAMA_PATH" serve > "$LOG_FILE" 2>&1 &
        
        # Capture the PID of the last backgrounded command
        PID=$!
        echo "$PID" > "$PID_FILE"
        echo "Ollama server started with PID: $PID"
        echo "Log file: $LOG_FILE"
    
        # Give it a moment to actually start and log
        sleep 2
        if is_running; then
            echo "Ollama server is running."
        else
            echo "Failed to start Ollama server. Check $LOG_FILE for details."
            rm -f "$PID_FILE" # Clean up the pid file if it failed
        fi
    }
    
    stop_ollama() {
        if ! is_running; then
            echo "Ollama server is not running."
            return 0
        fi
    
        PID=$(cat "$PID_FILE")
        echo "Stopping Ollama server (PID: $PID)..."
        kill "$PID"
        
        # Wait for the process to terminate
        RETRIES=10
        for i in $(seq 1 $RETRIES); do
            if ! is_running; then
                echo "Ollama server stopped."
                rm -f "$PID_FILE"
                return 0
            fi
            sleep 1
        done
    
        echo "Failed to stop Ollama server (PID: $PID) gracefully. Attempting to kill -9."
        kill -9 "$PID"
        sleep 1
        if ! is_running; then
            echo "Ollama server forcefully stopped."
            rm -f "$PID_FILE"
        else
            echo "Failed to stop Ollama server forcefully."
        fi
    }
    
    status_ollama() {
        if is_running; then
            echo "Ollama server is running (PID: $(cat "$PID_FILE"))."
            echo "Log file: $LOG_FILE"
        else
            echo "Ollama server is not running."
            if [ -f "$LOG_FILE" ]; then
                echo "Last log entries:"
                tail -n 10 "$LOG_FILE"
            fi
        fi
    }
    
    # --- Main Script Logic ---
    
    case "$1" in
        start)
            start_ollama
            ;;
        stop)
            stop_ollama
            ;;
        status)
            status_ollama
            ;;
        restart)
            stop_ollama
            start_ollama
            ;;
        log)
            if [ -f "$LOG_FILE" ]; then
                tail -f "$LOG_FILE"
            else
                echo "Log file not found: $LOG_FILE"
            fi
            ;;
        *)
            echo "Usage: $0 {start|stop|status|restart|log}"
            exit 1
            ;;
    esac
    
    exit 0
    

    How to Use:

    1. Save the script: Save the content above into a file, for example, ollama_server.sh.

    2. Make it executable:

      chmod +x ollama_server.sh
      
    3. Adjust OLLAMA_PATH: Make sure the OLLAMA_PATH variable at the top of the script points to the correct location of your ollama executable.

      • If you installed via brew install ollama, it's likely /usr/local/bin/ollama.
      • If you installed via the official installer, it might be in /usr/local/bin/ollama or ~/bin/ollama or similar. You can find it by typing which ollama in your terminal.
    4. Run commands:

      • Start the server:
        ./ollama_server.sh start
        
        This will detach Ollama from your terminal, print its PID, and put it in the background. You can close your terminal, and Ollama will continue running.
      • Check status:
        ./ollama_server.sh status
        
      • Stop the server:
        ./ollama_server.sh stop
        
      • View logs:
        ./ollama_server.sh log
        
        (This will tail -f the log file, so you'll see new log entries as they appear. Press Ctrl+C to exit.)

    Explanation:

    • nohup: nohup (no hang up) ensures that the process continues to run even if the user logs out or the terminal session closes.
    • > "$LOG_FILE" 2>&1: This redirects standard output (1) and standard error (2) to the specified LOG_FILE. This is crucial for debugging, as you won't see output in the terminal.
    • &: This puts the command into the background, allowing the script to continue immediately without waiting for ollama serve to finish.
    • PID=$!: After backgrounding a command with &, the special variable $! holds the Process ID (PID) of that backgrounded command.
    • echo "$PID" > "$PID_FILE": We store the PID in a file (~/.ollama/ollama.pid). This PID file is essential for the stop command to know which process to kill.
    • is_running function: This function checks if the PID file exists and if the process with that PID is actually running and is indeed ollama serve. This prevents accidentally killing the wrong process or attempting to stop a non-existent one.
    • kill "$PID": This sends a TERM signal to the process, asking it to gracefully shut down.
    • kill -9 "$PID": If a graceful shutdown fails, this sends a KILL signal, which forces the process to terminate immediately.

    This script provides a robust way to manage Ollama as a background service without using launchctl.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt