Systemd is a useful tool programmatically managing routine tasks. I encountered a specific situation with some constraints:
- start task on boot as a non-root user
- auto restart on failure
The solution to this issue is to create a systemd task as the non-root user.
systemctl edit --user --full --force <service_name>
[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
systemctl enable --user <service_name>
sudo loginctl enable-linger "$USER"
This is to allow the service continuing to run after the user is logged out.
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
Thanks to this issue