How to run minikube start as a linux service on startup?

This question is related to How to arrange a non admin command as a systemd service?

I have the following minikube.service file:

[Unit]
Description=Runs minikube on startup
After=vboxautostart-service.service vboxballoonctrl-service.service vboxdrv.service vboxweb-service.service

[Service]
Type=forking
ExecStart=/usr/local/bin/minikube start
User=mark
Group=mark

[Install]
WantedBy=multi-user.target

And it works fine when I start it manually from the shell:

mark@minikube-vm:~$ sudo systemctl start minikube.service
mark@minikube-vm:~$ minikube ip
192.168.99.100
mark@minikube-vm:~$ systemctl status minikube.service
● minikube.service - Runs minikube on startup
   Loaded: loaded (/etc/systemd/system/minikube.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-09-25 17:32:48 UTC; 34min ago
  Process: 8388 ExecStart=/usr/local/bin/minikube start (code=exited, status=0/SUCCESS)
    Tasks: 46 (limit: 19141)
   CGroup: /system.slice/minikube.service
           ├─8434 /usr/lib/virtualbox/VBoxXPCOMIPCD
           ├─8439 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
           ├─8618 /usr/lib/virtualbox/VBoxHeadless --comment minikube --startvm 6ec91432-0f09-4fdc-9976-6f736b15c37f --vrde config
           ├─8632 /usr/lib/virtualbox/VBoxNetDHCP --comment HostInterfaceNetworking-vboxnet0 --config /tmp/dhcp-config-kiyf3.xml
           └─8658 /usr/bin/pulseaudio --start --log-target=syslog

Sep 25 17:31:22 minikube-vm minikube[8388]: * Using the virtualbox driver based on existing profile
Sep 25 17:31:22 minikube-vm minikube[8388]: * Starting control plane node minikube in cluster minikube
Sep 25 17:31:22 minikube-vm minikube[8388]: * Restarting existing virtualbox VM for "minikube" ...
Sep 25 17:31:23 minikube-vm pulseaudio[8658]: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSu
Sep 25 17:31:23 minikube-vm pulseaudio[8658]: [pulseaudio] main.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported:
Sep 25 17:32:16 minikube-vm minikube[8388]: * Preparing Kubernetes v1.19.0 on Docker 19.03.12 ...
Sep 25 17:32:43 minikube-vm minikube[8388]: * Verifying Kubernetes components...
Sep 25 17:32:48 minikube-vm minikube[8388]: * Enabled addons: dashboard, default-storageclass, storage-provisioner
Sep 25 17:32:48 minikube-vm minikube[8388]: * Done! kubectl is now configured to use "minikube" by default
Sep 25 17:32:48 minikube-vm systemd[1]: Started Runs minikube on startup.
mark@minikube-vm:~$

But on machine boot it fails:

mark@minikube-vm:~$ systemctl status minikube.service
● minikube.service - Runs minikube on startup
   Loaded: loaded (/etc/systemd/system/minikube.service; enabled; vendor preset: enabled)
   Active: failed (Result: timeout) since Fri 2020-09-25 17:18:09 UTC; 12min ago
  Process: 7140 ExecStart=/usr/local/bin/minikube start (code=killed, signal=TERM)

Sep 25 17:16:39 minikube-vm minikube[7140]: * Using the virtualbox driver based on existing profile
Sep 25 17:16:39 minikube-vm minikube[7140]: * Starting control plane node minikube in cluster minikube
Sep 25 17:16:39 minikube-vm minikube[7140]: * Restarting existing virtualbox VM for "minikube" ...
Sep 25 17:16:40 minikube-vm pulseaudio[7438]: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSu
Sep 25 17:16:40 minikube-vm pulseaudio[7438]: [pulseaudio] main.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported:
Sep 25 17:17:34 minikube-vm minikube[7140]: * Preparing Kubernetes v1.19.0 on Docker 19.03.12 ...
Sep 25 17:18:06 minikube-vm minikube[7140]: * Verifying Kubernetes components...
Sep 25 17:18:08 minikube-vm systemd[1]: minikube.service: Start operation timed out. Terminating.
Sep 25 17:18:09 minikube-vm systemd[1]: minikube.service: Failed with result 'timeout'.
Sep 25 17:18:09 minikube-vm systemd[1]: Failed to start Runs minikube on startup.
mark@minikube-vm:~$

What am I missing?


Solution 1:

You've started a "service" which is basically just a script that runs some commands and then exits. This sort of thing should use Type=oneshot and RemainAfterExit=yes in a systemd unit, so that systemd knows the process will go away but still consider the "service" active.

You should also add a matching ExecStop= line that stops minikube, for completeness.

The systemd documentation has another example of such a unit.