#!/bin/sh killall swaybg PIDFILE="/tmp/randombg.pid" # Check if the PID file exists and if the process is running if [ -e "$PIDFILE" ] && kill -0 "$(bat "$PIDFILE")"; then echo "Another instance of the script is already running." kill -9 "$(bat $PIDFILE)" # Forcefully kill the old process fi echo $$ > "$PIDFILE" trap 'rm -f "$PIDFILE"; exit' INT TERM EXIT swaybg -i $(fd . /mnt/ssd/papes -t f | shuf -n1) -m fill & OLD_PID=$! while true; do sleep 300 swaybg -i $(fd . /mnt/ssd/papes -t f | shuf -n1) -m fill & NEXT_PID=$! sleep 5 kill $OLD_PID OLD_PID=$NEXT_PID done