Skip to content

Instantly share code, notes, and snippets.

@hpcdisrespecter
Created April 3, 2025 12:58
Show Gist options
  • Save hpcdisrespecter/6db3a3425adfa5709e8d1ab0ad0fad37 to your computer and use it in GitHub Desktop.
Save hpcdisrespecter/6db3a3425adfa5709e8d1ab0ad0fad37 to your computer and use it in GitHub Desktop.
Convert webp automation for systemd/openrc
#!/sbin/openrc-run
# /etc/init.d/webp-daemon
name="WebP Conversion Daemon"
description="Monitors and converts WebP files in home directory"
supervisor="supervise-daemon"
command="/usr/local/bin/webp-daemon.sh"
command_user="${RC_SVCNAME##*.}"
: ${command_user:=nobody}
# Set umask
umask 027
# Log file and pid path
output_log="/var/log/webp-daemon/webp-daemon.log"
error_log="/var/log/webp-daemon/error.log"
pidfile="/var/run/webp-daemon.pid"
# Dependency information
depend() {
need localmount
use logger
after bootmisc
}
# Service-specific settings
start_pre() {
checkpath -f -m 0644 -o "$command_user" "$output_log" "$error_log"
checkpath -d -m 0755 -o "$command_user" "$(dirname "$output_log")"
}
# Change limits if needed
#rc_ulimit="-n 4096"
# Respawn settings for supervise-daemon
respawn_delay=5
respawn_max=10
[Unit]
Description=WebP Conversion Daemon
After=network.target
[Service]
Type=simple
User=%i
ExecStart=/usr/local/bin/webp-daemon.sh
Restart=on-failure
RestartSec=5s
# Drop all capabilities and only retain what's needed
CapabilityBoundingSet=
# No capabilities needed for basic file operations in home directory
# when running as the user
# Additional security hardening
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=%h
PrivateTmp=true
PrivateDevices=true
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictAddressFamilies=AF_UNIX
RestrictNamespaces=true
LockPersonality=true
MemoryDenyWriteExecute=true
RestrictRealtime=true
[Install]
WantedBy=multi-user.target
#!/bin/bash
# webp-daemon.sh - A daemon that monitors for WebP files and converts them
# Required dependencies: inotify-tools, imagemagick
# Configuration
WATCH_DIR="$HOME"
LOG_FILE="/var/log/webp-daemon.log"
CONVERSION_FORMAT="png" # Target format
PID_FILE="/var/run/webp-daemon.pid"
# Function to log messages
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# Check for dependencies
if ! command -v inotifywait >/dev/null 2>&1; then
echo "Error: inotifywait is required. Install inotify-tools package."
exit 1
fi
if ! command -v magick >/dev/null 2>&1; then
echo "Error: magick is required. Install ImageMagick package."
exit 1
fi
# Create PID file
echo $$ > "$PID_FILE"
# Signal handling
cleanup() {
log "Daemon stopping..."
rm -f "$PID_FILE"
exit 0
}
trap cleanup SIGINT SIGTERM
# Initialize
log "Starting WebP conversion daemon, monitoring: $WATCH_DIR"
log "Converting WebP to $CONVERSION_FORMAT"
# Main loop
inotifywait -m -r -e close_write --format '%w%f' "$WATCH_DIR" | while read -r FILE
do
# Check if it's a WebP file
if [[ "$FILE" == *.webp ]]; then
log "Detected new WebP file: $FILE"
# Get file information
DIRNAME=$(dirname "$FILE")
BASENAME=$(basename "$FILE" .webp)
# Determine target filename with collision avoidance
TARGET="$DIRNAME/$BASENAME.$CONVERSION_FORMAT"
COUNTER=1
while [ -f "$TARGET" ]; do
TARGET="$DIRNAME/$BASENAME-$COUNTER.$CONVERSION_FORMAT"
COUNTER=$((COUNTER + 1))
done
# Convert the file
log "Converting to $TARGET"
if magick "$FILE" "$TARGET"; then
log "Conversion successful"
# Remove original WebP file
rm "$FILE"
log "Removed original WebP file: $FILE"
else
log "Error: Conversion failed for $FILE"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment