[GH-ISSUE #8439] Add a service file in /etc/init.d/ to support service start stop restart in self-package containers #5426

Closed
opened 2026-04-12 16:39:53 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @SunshineCloudTech on GitHub (Jan 15, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/8439

Title: Add a service file in /etc/init.d/ to support service start stop restart in self-package containers

Hi,I am looking for a Method to install ollama in a container, and use service start stop restart to manage the ollama backend,
I write a sample file in /etc/init.d/ollama,to support service start stop restart ollama, But it takes error

#!/bin/bash
# Ollama Service Manager Script
# Place this file in /etc/init.d/ollama and make it executable.

### BEGIN INIT INFO
# Provides:          ollama
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage Ollama service
# Description:       This script starts, stops, and restarts the Ollama service.
### END INIT INFO

SERVICE_NAME="Ollama"
OLLAMA_COMMAND="/usr/bin/ollama" # Update with the path to the Ollama serve command
PID_FILE="/var/run/ollama/ollama.pid"
USER="ollama"
GROUP="ollama"

start_service() {
    echo "Starting $SERVICE_NAME..."
    if [ -f "$PID_FILE" ]; then
        echo "$SERVICE_NAME is already running."
        return 1
    fi
    su -s /bin/bash -c "nohup $OLLAMA_COMMAND serve> /var/log/ollama/ollama.log 2>&1 & echo \$! > $PID_FILE" $USER
    echo "$SERVICE_NAME started with PID $(cat $PID_FILE)."
}

stop_service() {
    echo "Stopping $SERVICE_NAME..."
    if [ ! -f "$PID_FILE" ]; then
        echo "$SERVICE_NAME is not running."
        return 1
    fi
    kill $(cat "$PID_FILE")
    rm -f "$PID_FILE"
    echo "$SERVICE_NAME stopped."
}

restart_service() {
    echo "Restarting $SERVICE_NAME..."
    stop_service
    sleep 1
    start_service
}

case "$1" in
    start)
        start_service
        ;;
    stop)
        stop_service
        ;;
    restart)
        restart_service
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

exit 0

Error

Starting Ollama...
bash: line 1: /var/run/ollama/ollama.pid: No such file or directory
cat: /var/run/ollama/ollama.pid: No such file or directory
Ollama started with PID .

can anyone help me solve the problem

Originally created by @SunshineCloudTech on GitHub (Jan 15, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/8439 Title: Add a service file in /etc/init.d/ to support service start stop restart in self-package containers Hi,I am looking for a Method to install ollama in a container, and use service start stop restart to manage the ollama backend, I write a sample file in /etc/init.d/ollama,to support service start stop restart ollama, But it takes error ```bash #!/bin/bash # Ollama Service Manager Script # Place this file in /etc/init.d/ollama and make it executable. ### BEGIN INIT INFO # Provides: ollama # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Manage Ollama service # Description: This script starts, stops, and restarts the Ollama service. ### END INIT INFO SERVICE_NAME="Ollama" OLLAMA_COMMAND="/usr/bin/ollama" # Update with the path to the Ollama serve command PID_FILE="/var/run/ollama/ollama.pid" USER="ollama" GROUP="ollama" start_service() { echo "Starting $SERVICE_NAME..." if [ -f "$PID_FILE" ]; then echo "$SERVICE_NAME is already running." return 1 fi su -s /bin/bash -c "nohup $OLLAMA_COMMAND serve> /var/log/ollama/ollama.log 2>&1 & echo \$! > $PID_FILE" $USER echo "$SERVICE_NAME started with PID $(cat $PID_FILE)." } stop_service() { echo "Stopping $SERVICE_NAME..." if [ ! -f "$PID_FILE" ]; then echo "$SERVICE_NAME is not running." return 1 fi kill $(cat "$PID_FILE") rm -f "$PID_FILE" echo "$SERVICE_NAME stopped." } restart_service() { echo "Restarting $SERVICE_NAME..." stop_service sleep 1 start_service } case "$1" in start) start_service ;; stop) stop_service ;; restart) restart_service ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 ;; esac exit 0 ``` Error ```bash Starting Ollama... bash: line 1: /var/run/ollama/ollama.pid: No such file or directory cat: /var/run/ollama/ollama.pid: No such file or directory Ollama started with PID . ``` can anyone help me solve the problem
GiteaMirror added the feature request label 2026-04-12 16:39:53 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#5426