Here is an idea on this issue. Created a bash script that checks the size of a specified file. If the file size is not zero, it stops a specified service. If the file size is zero, it checks the status of the service, and if the service is stopped, it starts the service. The script also logs actions to a log file.
?
[root@wb1gof my_scripts]# cat monlink_g2link_check.sh
=========================================
#!/bin/bash
# Script to check file size and manage service status
# Configuration
FILE_PATH="/root/g2_link/RPT_STATUS.txt" ?# Change this to the file you want to check
SERVICE_NAME="monlink" ? ? ?# Change this to your service name
LOG_FILE="/var/log/monlink_g2_link_check.log" ?# Change this to your desired log file location
# Function to log messages
log_message() {
? ? local message="$1"
? ? echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE"
}
# Check if the file exists
if [ ! -f "$FILE_PATH" ]; then
? ? log_message "Error: File '$FILE_PATH' does not exist."
? ? exit 1
fi
# Get the file size
FILE_SIZE=$(stat -c%s "$FILE_PATH")
# Check if the file size is not zero
if [ "$FILE_SIZE" -ne 0 ]; then
? ? log_message "File size is not zero ($FILE_SIZE bytes). Stopping service '$SERVICE_NAME'."
? ? systemctl stop "$SERVICE_NAME"
? ? if [ $? -eq 0 ]; then
? ? ? ? log_message "Service '$SERVICE_NAME' stopped successfully."
? ? else
? ? ? ? log_message "Error: Failed to stop service '$SERVICE_NAME'."
? ? ? ? exit 1
? ? fi
else
? ? log_message "File size is zero. Checking status of service '$SERVICE_NAME'."
? ? SERVICE_STATUS=$(systemctl is-active "$SERVICE_NAME")
? ? if [ "$SERVICE_STATUS" = "inactive" ]; then
? ? ? ? log_message "Service '$SERVICE_NAME' is stopped. Starting service."
? ? ? ? systemctl start "$SERVICE_NAME"
? ? ? ? if [ $? -eq 0 ]; then
? ? ? ? ? ? log_message "Service '$SERVICE_NAME' started successfully."
? ? ? ? else
? ? ? ? ? ? log_message "Error: Failed to start service '$SERVICE_NAME'."
? ? ? ? ? ? exit 1
? ? ? ? fi
? ? else
? ? ? ? log_message "Service '$SERVICE_NAME' is already running."
? ? fi
fi
exit 0
=========================================
Not sure what is wrong just yet... but when I check the RPT_STATUS.txt file size with an ls -l, it shows 0 but the script says it is not empty?
-rw-r--r-- 1 root root ? ? ? 0 Apr ?1 13:52 RPT_STATUS.txt
?
2025-04-01 13:52:01 - File size is not zero (38 bytes). Stopping service 'monlink'.
2025-04-01 13:52:01 - Service 'monlink' stopped successfully.
?
Maybe the way I check the file size?
?
Terry
?
?
--
Terry Stader - KA8SCP
D-STAR Gateway & Reflector Admin