post
Converting Ollama Systemd Service to SysVinit Script: A simple Guide
Systemd and SysVinit represent two different approaches to init systems in Linux, managing how services start, stop, and manage dependencies. While Systemd has become the standard in many modern distributions, SysVinit remains widely used, especially in systems prioritizing stability and simplicity, or where Systemd is not available or desired. This guide will walk you through converting the Ollama Systemd service into a SysVinit script.
The Ollama Systemd Service
Let's consider the original Systemd service unit for the Ollama Service:
[Unit]
Description=Ollama Service
After=network-online.target
[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="PATH=/usr/local/bin:/usr/bin:/bin"
[Install]
WantedBy=default.targetThis unit file defines a service that starts after the network is online, runs a command as a specific user and group, and ensures the service restarts if it fails.
Converting to a SysVinit Script
Use your preferred editor to create the SysVinit script. I'll use nano. Make sure you have sudo privileges.
sudo nano /etc/init.d/ollamaNow add
Script Header
Start with the script's metadata, specifying when the service should start and stop, its description, and dependencies.
#!/bin/sh
### BEGIN INIT INFO
# Provides: ollama
# Required-Start: $network
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Ollama Service
# Description: Starts the Ollama Service at boot
### END INIT INFODefine Variables
Set variables for the daemon's name, executable path, and PID file location for easier management and readability.
. /lib/lsb/init-functions
NAME=ollama
DAEMON=/usr/local/bin/ollama
PIDFILE=/var/run/$NAME.pidCase Statement
Implement a case statement to handle start, stop, restart, and status commands, using start-stop-daemon for process management.
case "$1" in
start)
log_daemon_msg "Starting Ollama Service"
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $DAEMON -- serve --chuid ollama:ollama
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping Ollama Service"
start-stop-daemon --stop --quiet --pidfile $PIDFILE
log_end_msg $?
;;
restart)
$0 stop
$0 start
;;
status)
status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}"
exit 1
;;
esac
Save, exit the editor and ensure the script is executable and register it with the system's init management to start at boot.
chmod +x /etc/init.d/ollama
update-rc.d ollama defaultsStarting the Ollama Service
To start the Ollama service, you'll need to run the following command:
sudo /etc/init.d/ollama status
To verify that the service started successfully, you can use the status argument with your SysVinit script:
sudo /etc/init.d/ollama status
[sudo] password for user
Checking ollama... RunningThat's it. The Ollama service will now run on systems using SysVinit like MX Linux for example.
Archived comments
These comments were imported from the previous site. New comments are not available.
jorge
areglo
Joril
Chris