Skip to content

Instantly share code, notes, and snippets.

@stephankoelle
Created June 17, 2025 09:45
Show Gist options
  • Select an option

  • Save stephankoelle/5b154d13159893c663e6e2395deb2959 to your computer and use it in GitHub Desktop.

Select an option

Save stephankoelle/5b154d13159893c663e6e2395deb2959 to your computer and use it in GitHub Desktop.
run fleetlock server only when someone can handle emergencies
#~/.config/systemd/user/manage_fleetlock.service
#journalctl --user -t manage_fleetlock
[Unit]
Description=Manage fleetlock service based on public holidays and work hours
[Service]
Type=oneshot
ExecStart=/usr/local/bin/manage_fleetlock.sh
#!/bin/bash
# /usr/local/bin/manage_fleetlock.sh
#check with nager.at if ther is a public holiday
set -x
# Define the service name
SERVICE_NAME="fleetlock.service"
LOG_TAG="manage_fleetlock"
# Log a message to the user log
log_message() {
local message="$1"
logger -t "$LOG_TAG" "$message"
}
# Check if today is a public holiday in Baden-Württemberg
# 204 is error for bash?
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" 'https://date.nager.at/api/v3/IsTodayPublicHoliday/DE?countyCode=DE-BW&offset=1')
# Log the response code
log_message "Received HTTP response code: $RESPONSE"
# Check if today is a public holiday
if [ "$RESPONSE" -eq 204 ]; then
# Check the current day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
DAY_OF_WEEK=$(date %u)
# Log the day of the week
log_message "Current day of the week: $DAY_OF_WEEK"
# Check if today is a workday (Monday to Friday)
if [ "$DAY_OF_WEEK" -ge 1 ] && [ "$DAY_OF_WEEK" -le 5 ]; then
# Get the current hour
CURRENT_HOUR=$(date %H)
# Log the current hour
log_message "Current hour: $CURRENT_HOUR"
if [ "$CURRENT_HOUR" -ge 8 ] && [ "$CURRENT_HOUR" -lt 12 ]; then #10-14 in utc
# Check if the service is not running and start it if needed
if ! systemctl --user is-active --quiet "$SERVICE_NAME"; then
log_message "Starting $SERVICE_NAME because it's within working hours."
systemctl --user start "$SERVICE_NAME"
else
log_message "$SERVICE_NAME is already running."
fi
else
# Check if the service is running and stop it if needed
if systemctl --user is-active --quiet "$SERVICE_NAME"; then
log_message "Stopping $SERVICE_NAME because it's outside working hours."
systemctl --user stop "$SERVICE_NAME"
else
log_message "$SERVICE_NAME is not running."
fi
fi
else
# Check if the service is running and stop it if needed
if systemctl --user is-active --quiet "$SERVICE_NAME"; then
log_message "Stopping $SERVICE_NAME because it's a weekend."
systemctl --user stop "$SERVICE_NAME"
else
log_message "$SERVICE_NAME is not running."
fi
fi
else
# Check if the service is running and stop it if needed
if systemctl --user is-active --quiet "$SERVICE_NAME"; then
log_message "Stopping $SERVICE_NAME because today is a public holiday."
systemctl --user stop "$SERVICE_NAME"
else
log_message "$SERVICE_NAME is not running."
fi
fi
#~/.config/systemd/user/manage_fleetlock.timer
[Unit]
Description=Runs manage_fleetlock service every hour
[Timer]
OnBootSec=5min
OnUnitActiveSec=15m
#1h
Unit=manage_fleetlock.service
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment