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”).
Note: This script enforces a soft limit. It prevents new Firefox windows from opening once your daily time quota is reached but does not close an already running Firefox session. This design avoids accidental data loss.
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=
, orIcon=
, it is necessary to log out and back in.
5. Set alias for this specific version
Keep the original Firefox untouched and use a new alias/script for the limited version.
Open shell configuration file (.bashrc or .zshrc) .
nano ~/.bashrc
And add this to the file.
alias firefox-l="$HOME/bin/firefox-limit.sh"
firefox-l
is now the limited Firefox command.- The original
firefox
command still points to the real Firefox binary.
After editing the file, reload it:
source ~/.bashrc # or ~/.zshrc if you use zsh