systemd: “Environment” directive to set PATH

You can’t use EnvVars in Environment directives. The whole Environment= will be ignored. If you use EnvironmentFile=, then the specified file will be loaded without substitution. So PATH=/local/bin:$PATH would be exactly that, and this is probably not what you want. Under CentOS7 the following works. # /etc/systemd/system/nagios.service.d/env.conf [Service] Environment=”PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin” > sudo systemctl daemon-reload > sudo … Read more

python import of local module failing when run as systemd/systemctl service

I had a very similar issue converting an upstart heartbeat.conf to a systemd heartbeat.service, except with the requests module. The solution was to specify in the new .service what user to run it as: [Unit] Description=web server monitor [Service] WorkingDirectory=/home/<user>/ User=<user> ExecStart=/home/<user>/heartbeat.py Restart=always [Install] WantedBy=multi-user.target Without the User=<user>, I was getting in the journalctl: systemd[1]: … Read more

When should the option RemainAfterExit needs to be set true when creating new systemd services?

Use RemainAfterExit=yes for services, which somehow change state of the system. When you want that state reverted, you just stop the service. Then you can start it again, but not without first stopping it. An example would be service which creates a flag in filesystem to be used by some other application. When started, it … Read more

How to restart a service if its dependent service is restarted

You can use PartOf. [Unit] After=foo.service Requires=foo.service PartOf=foo.service From the systemd.unit man page: PartOf= Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit … Read more

How can I install a systemd service using Ansible?

I use the template module to install the .service file into the /etc/systemd/system. According to this digital ocean blog post /lib/systemd/system should be reserved for packages bundled with the OS itself, and third party services should be defined in /etc/systemd/system. With ansible’s systemd module I’d start the service with daemon_reload=yes. Prior to Ansible 2.2: I … Read more

How to set up a systemd service to retry 5 times on a cycle of 30 seconds

To allow a maximum of 5 retries separated by 30 seconds use the following options in the relevant systemd service file. [Unit] StartLimitInterval=200 StartLimitBurst=5 [Service] Restart=always RestartSec=30 This worked for a service that runs a script using Type=idle. Note that StartLimitInterval must be greater than RestartSec * StartLimitBurst otherwise the service will be restarted indefinitely. … Read more