mac CLI. how can I start ollama server via script without a user on the terminal? I…
- User
mac CLI. how can I start ollama server via script without a user on the terminal? I don't want to use launchctl
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
nohupand 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, andstatusfunctionalities:#!/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 0How to Use:
-
Save the script: Save the content above into a file, for example,
ollama_server.sh. -
Make it executable:
chmod +x ollama_server.sh -
Adjust
OLLAMA_PATH: Make sure theOLLAMA_PATHvariable at the top of the script points to the correct location of yourollamaexecutable.- 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/ollamaor~/bin/ollamaor similar. You can find it by typingwhich ollamain your terminal.
- If you installed via
-
Run commands:
- Start the server:
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../ollama_server.sh start - Check status:
./ollama_server.sh status - Stop the server:
./ollama_server.sh stop - View logs:
(This will./ollama_server.sh logtail -fthe log file, so you'll see new log entries as they appear. PressCtrl+Cto exit.)
- Start the server:
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 specifiedLOG_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 forollama serveto 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 thestopcommand to know which process to kill.is_runningfunction: This function checks if the PID file exists and if the process with that PID is actually running and is indeedollama serve. This prevents accidentally killing the wrong process or attempting to stop a non-existent one.kill "$PID": This sends aTERMsignal to the process, asking it to gracefully shut down.kill -9 "$PID": If a graceful shutdown fails, this sends aKILLsignal, which forces the process to terminate immediately.
This script provides a robust way to manage Ollama as a background service without using
launchctl.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.