DEV Community

onichandame
onichandame

Posted on • Edited on

1 2

Rootless systemd and podman

Systemd is a useful tool programmatically managing routine tasks. I encountered a specific situation with some constraints:

  1. start task on boot as a non-root user
  2. auto restart on failure

The solution to this issue is to create a systemd task as the non-root user.

Steps

Create the service

systemctl edit --user --full --force <service_name>

add the content

[Unit]
Description=<Service>

[Service]
Restart=always
ExecStart=<Servece_Start>
ExecStop=<Service_Stop>

[Install]
WantedBy=default.target

the last line registers the service to the dependency list for auto-start

enable the service

systemctl enable --user <service_name>

(optional) enable linger

 sudo loginctl enable-linger "$USER"

This is to allow the service continuing to run after the user is logged out.

Podman Service

When running podman container as a systemd service, the ExecStart command should be podman start -a <container_name> and the ExecStop command should be podman stop -t <seconds_to_wait> <container_name>. Details can be found at https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux_atomic_host/7/html/managing_containers/running_containers_as_systemd_services_with_podman

Credits

Thanks to this issue

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay