How to send an email alert when a linux service has stopped?
Linux operating systems are jam packed with features, there are lots of command line tools avilable inside and in the package repository. You'd always find a program for most of your use cases. In this post you'll learn about some of the important features the linux family of operating systems have to offer.
There are 2 suggested solutions in this post and each one is listed below with a detailed description on the basis of most helpful answers as shared by the users. I have tried to cover all the aspects as briefly as possible covering topics such as Linux, Systemd and a few others. I have categorized the possible solutions in sections for a clear and precise explanation. Please consider going through all the sections to better understand the solutions.
You can use the
ExecStopPost setting in your service configuration, as documented in systemd.service to send an email whenever the service stops. If you don't want to receive an email when the service exits cleanly, you can use the
SERVICE_RESULT environment variable (documented in systemd.exec) in the script sending the email.
Here's the final solution you can try out in case no other solution was helpful to you. This one's applicable and useful in some cases and could possiblty be of some help. No worries if you're unsure about it but I'd recommend going through it.
You can have a short bash script that checks the status of the service and pushes a notification if it is not running. For pushing notifications you can use Pushover which is pretty easy to integrate with a bash script after generating security keys.
Your bash script can be added to the
crontab for periodic execution say every 5 minutes(basically whatever period you want).