MongoDB status script

This commit is contained in:
2023-01-24 11:50:13 +13:00
parent b00745bac6
commit f66f06ce51

22
mongodb-status.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/bash
# This script checks the status of the mongodb service, and if it is not running or contains the text "Active: failed",
# it restarts the service and writes the current date, time, output and process ID if available to a log file,
# limiting the log file to a maximum of 1000 lines
LOG_FILE="./logs/mongodb-status.log"
status=$(sudo systemctl status mongod)
current_time=$(date +"%Y-%m-%d %T")
if echo "$status" | grep -qE "Active: running|Active: active"; then
echo "[$current_time] Success $(echo "$status" | grep "Main PID")" >> $LOG_FILE
else
echo "[$current_time] Failed" >> $LOG_FILE
fi
lines=$(wc -l < $LOG_FILE)
if [ $lines -gt 1000 ]; then
tail -n +2 $LOG_FILE > "$LOG_FILE.tmp"
mv "$LOG_FILE.tmp" $LOG_FILE
fi