01 Aug 2020
When you want to setup systemd to send mail when a service failure, you may find a similar solution on the the internet like this:
[Unit] Description=%i failure email notification [Service] Type=oneshot ExeStart=/bin/bash -c "mail -s "%i service failed" email@example.com
But when you do some test on it. It never success. It took me some times to find why.
KillMode= … If set to control-group, all remaining processes in the control group of this unit will be killed on unit stop (for services: after the stop command is executed, as configured with ExecStop=). If set to mixed, the SIGTERM signal (see below) is sent to the main process while the subsequent SIGKILL signal (see below) is sent to all remaining processes of the unit’s control group. If set to process, only the main process itself is killed (not recommended!). If set to none, no process is killed (strongly recommended against!). In this case, only the stop command will be executed on unit stop, but no process will be killed otherwise. Processes remaining alive after stop are left in their control group and the control group continues to exist after stop unless empty. … Defaults to control-group
As the above description, you can find that it’s caused by the systemd default systemd killMode.
Because mail sending default is async. So when the oneshot systemd service finished call the mail, it will kill all spawned child process include the mail process.
So, we just need to change the KillMode to process mode then everything is ok.
[Unit] Description=%i failure email notification [Service] Type=oneshot ExeStart=/bin/bash -c "mail -s "%i service failed" firstname.lastname@example.org KillMode=process