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.target
This 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/ollama
Now 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 INFO
Define 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.pid
Case 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 defaults
Starting 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... Running
That’s it. The Ollama service will now run on systems using SysVinit like MX Linux for example.
in the systemd case of the [Service] variable
… Environment=”OLLAMA_HOST=0.0.0.0″, where would it be set for sysvinit
How do I configure Ollama server?.
Thanks
Try adding the following before the case statement:
OLLAMA_HOST=”0.0.0.0″
export OLLAMA_HOST
Great, thanks!
A little correction though: the “–chuid ollama:ollama” should go BEFORE the “– serve” (at least on my Devuan 4)
A bit of a better start that lets you set up logging:
Extra variables:
DAEMONUSER=ollama
LOGFILE=/var/log/ollama/ollama.log
Start command:
start-stop-daemon –start –pidfile $PIDFILE –make-pidfile –background –chuid $DAEMONUSER –startas /bin/bash — -c “exec $DAEMON serve >> $LOGFILE 2>&1”
Note: You need to create a ollama user and create the log file location and make it owned by the ollama user. Start and Stop work fine. An entry should also be made in logrotate to rotate the log.