Revised problem description and my attempt to help people build a
"workaround":

The crux of the problem:

Timeshift leaves a mess when it crashes, which apparently happens pretty
often, and once that happens, the mess prevents Timeshift from ever
running again, because it thinks “another instance is running” (see
Timeshift’s own logs).

I am learning that this problem is more complex and more widespread
(e.g. it apparently also affects recent versions of Linux Mint; see
reports on GitHub) than it first seemed to me. ChatGPT helped me make a
"watchdog" script (see below) and /etc/cron.d entry that triggers it at
HH:20 to clean up any messes, log what it finds, and "archive" crash
files so they don't get overwritten next time Timeshift crashes. I think
it works, it has cleaned up after several crashes and Timeshift runs
again, but I don't fully understand it, so I submit it here ...

... ONLY FOR IDEAS; PLEASE VERIFY BEFORE USE !

Here is a POSSIBLY IMPERFECT “Timeshift Watchdog” script to clean up the
mess Timeshift leaves when it crashes, so Timeshift can run again when
later triggered. Trigger this script to run at e.g. HH:20 via e.g. a
file in /etc/cron.d.

---------------  /usr/local/sbin/timeshift-watchdog.sh  ---------------

#!/bin/bash

# ---- Configuration ----
BASE_DIR="/var/log/timeshift-watchdog"
LOGFILE="$BASE_DIR/watchdog.log"
ARCHIVE_DIR="$BASE_DIR/crash-archive"
MAILTO="james"
HOST="$(hostname)"

mkdir -p "$BASE_DIR"
mkdir -p "$ARCHIVE_DIR"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOGFILE"
}

log "Watchdog check started."

CRASH_DETECTED=0
ARCHIVED_FILES=""

# ---- If Timeshift is currently running, exit safely ----
if pgrep -x timeshift > /dev/null; then
    log "Timeshift running normally. Exiting."
    exit 0
fi

# ---- Check for stale mounts ----
MOUNTS=$(mount | awk '/\/run\/timeshift/ {print $3}')

if [ -n "$MOUNTS" ]; then
    CRASH_DETECTED=1
    log "CRASH DETECTED: stale mounts present."

    for m in $MOUNTS; do
        log "Unmounting $m"
        umount -l "$m" 2>>"$LOGFILE"
    done

    log "Stale mount cleanup complete."
fi

# ---- Check for stale PID directories (independent of mounts) ----
for d in /run/timeshift/*; do
    [ -d "$d" ] || continue
    PID=$(basename "$d")

    if ! ps -p "$PID" > /dev/null 2>&1; then
        CRASH_DETECTED=1
        log "Stale PID directory detected: $PID — removing."
        rm -rf "$d"
    fi
done

# ---- Archive crash files if present ----
STAMP="$(date '+%Y%m%d-%H%M%S')"

for f in /var/crash/*timeshift*; do
    [ -f "$f" ] || continue
    CRASH_DETECTED=1
    BASENAME=$(basename "$f")
    DEST="$ARCHIVE_DIR/${BASENAME}-$STAMP"
    mv "$f" "$DEST"
    ARCHIVED_FILES="$ARCHIVED_FILES\n$DEST"
    log "Archived crash file to $DEST"
done

# ---- Notify if intervention occurred ----
if [ "$CRASH_DETECTED" -eq 1 ]; then
    log "Cleanup complete. Sending notification email."

    echo -e "Timeshift crash condition detected and cleaned on $HOST at $(date 
'+%Y-%m-%d %H:%M:%S').\n\nArchived crash files:$ARCHIVED_FILES\n\nSee $LOGFILE 
for details." \
        | mail -s "Timeshift cleanup performed on $HOST" "$MAILTO"
else
    log "No crash condition detected."
fi

exit 0

------  I trigger timeshift-watchdog.sh with this file in /etc/cron.d:  ------
 
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO="james"

20 * * * * root /usr/local/sbin/timeshift-watchdog.sh "cron.d at HH:20 -
Jim"

- end -

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2141449

Title:
  Timeshift scheduled snapshots silently fail due to incorrect cron
  command, misleading GUI, and lack of error reporting

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/timeshift/+bug/2141449/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to