Allison is coding...

Limiting Application Time on Linux (Firefox Example)

1. Intention

The goal is to restrict the daily usage time of an entertainment application (e.g., Firefox) while still keeping it accessible from the application launcher (“Show Applications”).

2. About the script

A small wrapper script enforces the time limit. Each time the application launches, the script checks accumulated usage time (stored in a log) and either allows the launch or blocks it once the limit is exceeded. Example script (~/bin/firefox-limited):

#!/bin/bash

LOGFILE="$HOME/.firefox_time.log"
LIMIT=3600   # seconds per day (1h)

TODAY=$(date +%F)
NOW=$(date +%s)

# Initialize log if missing
if [ ! -f "$LOGFILE" ]; then
  echo "$TODAY 0 $NOW" > "$LOGFILE"
fi

read LOGDATE USED LASTSTART < "$LOGFILE"

# Reset if day changed
if [ "$LOGDATE" != "$TODAY" ]; then
  USED=0
fi

if [ "$USED" -ge "$LIMIT" ]; then
  notify-send "Time limit reached for Firefox"
  exit 1
fi

# Launch Firefox and track usage
/usr/bin/firefox "$@" &
PID=$!

# Record session time when it ends
wait $PID
END=$(date +%s)
SESSION=$((END - NOW))
USED=$((USED + SESSION))
echo "$TODAY $USED $NOW" > "$LOGFILE"

Make it executable:

chmod +x ~/bin/firefox-limited

Extra command to check usage so far:

awk '{print "Used today: " $2 " seconds"}' ~/.firefox_time.log

Or just:

cat ~/.firefox_time.log

3. Replacing Firefox in the application menu (Ubuntu, Snap install)

Ubuntu’s Firefox Snap places its .desktop file at:

/var/lib/snapd/desktop/applications/firefox_firefox.desktop

Copy it to the user directory for editing:

cp /var/lib/snapd/desktop/applications/firefox_firefox.desktop ~/.local/share/applications/

Open the copy and edit the Exec= line:

  • Original (Snap default):

    Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/firefox_firefox.desktop /snap/bin/firefox %u
    
  • Replace with the wrapper:

    Exec=/home/USERNAME/bin/firefox-limited %u
    

Keeping %u ensures Firefox can still accept a URL argument, for example when opening a link from another application.

4. Refreshing the desktop entry

  • Running update-desktop-database only updates MIME type caches; it will not refresh menu entries.
  • To see changes to Exec=, Name=, or Icon=, it is necessary to log out and back in.