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

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

What is the difference between systemd’s ‘oneshot’ and ‘simple’ service types?

The Type=oneshot service unit: blocks on a start operation until the first process exits, and its state will be reported as “activating”; once the first process exits, transitions from “activating” straight to “inactive”, unless RemainAfterExit=true is set (in which case it becomes “active” with no processes!); may have any number (0 or more) of ExecStart= … Read more

How does the systemd stop command actually work?

By default, a SIGTERM is sent, followed by 90 seconds of waiting followed by a SIGKILL. Killing processes with systemd is very customizable and well-documented. I recommend reading all of man systemd.kill as well as reading about ExecStop= in man systemd.service. To respond to those signals, refer to the signal handling documentation for the language … 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

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)