Post

Using Systemd timer to keep service running

Overview

This guide explains how to use systemd timer to keep service running. During some sutiuations, you may need to restart the service after some time. or some service is crashing. This is where systemd timer comes in.

I have a test server, I need to monitor mongodb service and keep it always active. Usually, the crontab is a good solution. Today I will show how to use systemd timer to keep the service running.

Steps

  1. Create systemd service

    1
    
     sudo vim /etc/systemd/system/check_mongodb.service
    
    1
    2
    3
    4
    5
    6
    
     [Unit]
     Description=Check MongoDB Service
     After=network.target
    
     [Service]
     ExecStart=/home/shells/check_mongo.sh
    
  2. Create systemd timer

    1
    
     sudo vim /etc/systemd/system/check_mongodb.timer
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
     [Unit]
     Description=Run Check MongoDB Service every minute
    
     [Timer]
     OnBootSec=1min
     OnUnitActiveSec=1min
     Unit=check_mongo.service
    
     [Install]
     WantedBy=timers.target
    
  3. Create check_mongo.sh

    I have used the following command to create the check_mongo.sh:

    1
    
     sudo vim /home/shells/check_mongo.sh
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
     #!/bin/bash
    
     LOGFILE="/var/log/check_mongo.log"
     TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
    
     # Check if MongoDB is running
     if ! pgrep -x "mongod" > /dev/null
     then
         echo "$TIMESTAMP - MongoDB is not running. Starting MongoDB..." | tee -a $LOGFILE
         # Send notification
         curl -d "The MongoDB service has crashed. Please monitor its status." ntfy.sh/mytopic
         # Start MongoDB
         systemctl start mongod
         if [ $? -eq 0 ]; then
             echo "$TIMESTAMP - MongoDB service started successfully." | tee -a $LOGFILE
         else
             echo "$TIMESTAMP - Failed to start MongoDB service." | tee -a $LOGFILE
             # Send status notification
             curl -d "Failed to start MongoDB service, please monitor the status." ntfy.sh/mytopic
         fi
     fi
    
  4. Enable and start timer

    1
    2
    
     sudo systemctl enable check_mongodb.timer
     sudo systemctl start check_mongodb.timer
    
  5. Enable and start service

    1
    2
    
     sudo systemctl enable check_mongodb.service
     sudo systemctl start check_mongodb.service
    
This post is licensed under CC BY 4.0 by the author.