In this article, I’ll guide you through the process of creating a simple service startup job in Linux, using systemd. This method is particularly useful for both standard applications and rootless containers managed by Podman.

Sample systemd file

Below is a sample service file for systemd:

[Unit]
Description=Description for application
After=network.target

[Service]
User=<user>
Group=<group>
WorkingDirectory=<path where running file is located>
Environment=<path with environment variable PATH "PATH=/location">
EnvironmentFile=<path to environment file>
ExecStart=<a command with necessary parameters to start application>

[Install]
WantedBy=multi-user.target

Replace <user>, <group>, <path where running file is located>, <path with environment variable>, <path to environment file>, and <a command with necessary parameters to start application> with the appropriate values for your application.

Running and activating tasks

Place the service file in /etc/systemd/system. Name the file as you wish, but ensure it has a .service extension, like testapp.service.

Reload the systemd daemon after creating the file:

sudo systemctl daemon-reload

Start the application:

sudo systemctl start testapp.service

To have the application start automatically at boot:

sudo systemctl enable testapp.service

Running Rootless Containers with Podman

For automating the start and stop of a rootless container with Podman, follow these steps:

  1. Generate the systemd unit file and update it:

Use Podman to generate a systemd unit file:

[admin@podman ~]$ podman generate systemd --new --files --name registry
/home/admin/container-registry.service

This command will create a file like container-registry.service. You can then customize this file as needed. For instance:


# container-registry2.service

# autogenerated by Podman 4.0.2

# Mon Jun  6 19:38:56 CEST 2022

[Unit]
Description=Podman container-registry2.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=%t/containers

[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStartPre=/bin/rm -f %t/%n.ctr-id
ExecStart=/usr/bin/podman start registry2
ExecStop=/usr/bin/podman stop registry2
Type=notify
NotifyAccess=all

[Install]
WantedBy=default.target

  1. Install the generated unit file:

For rootless container I need to store the unit file in ~/.config/systemd/user/. For the root container copy the file to /etc/systemd/system/.

cp container-registry.service ~/.config/systemd/user/registry.service

Enable the service with the --user flag:

[admin@podman ~]$ systemctl --user enable registry.service
  1. Enable systemd login persistence:

To ensure the service remains active after logout, enable lingering for your user:

loginctl enable-linger <username>

Replace <username> with your actual username.

And that’s it! You now have a systemd service for your application and a Podman rootless container, both configured for automatic startup and management.

This method provides a seamless and efficient way to manage application and container startups in a Linux environment, ensuring that your services are always up and running as needed.

Reference