The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 4,273β€…    Votes:  16β€…    βœ… Solution
Tags: 16.04   lock-screen   dbus   gnome-screensaver   multi-timer  
Link: πŸ” See Original Answer on Ask Ubuntu ⧉ πŸ”—

URL: https://askubuntu.com/q/837115
Title: Application that will lock screen after a set amount of time for Ubuntu
ID: /2016/10/14/Application-that-will-lock-screen-after-a-set-amount-of-time-for-Ubuntu
Created: October 14, 2016    Edited:  January 9, 2022
Upload: September 15, 2024    Layout:  post
TOC: true    Navigation:  true    Copy to clipboard:  false


ToC Skip

June 4, 2018 Update

A much superior program called multi-timer has been created: Set of countdown timers with alarm

Top ToS Skip

Table of Contents

Top ToS ToC Skip

Lock Screen Timer

systray.gif

Top ToS ToC Skip

Create your own Lock Screen Timer instead of 3rd Party applications

Although there are 3rd Party applications to do this, you can create your own. Summary of steps:

Top ToS ToC Skip

Revision history

Edit 1 (Nov 19, 2016): Final version. Impending lock warnings at 15, 10, 5, 3, 2 and 1 minute(s) remaining using message and sound. Use zenity to get number of minutes (defaults to 30).

Edit 2 (Nov 20,2016): Code change to kill previous sleeping script.

Edit 3 (Jan 20,2017): Revise killing previous script code. Add information message if previous version was already running and terminated.

Edit 4 (Feb 4,2017): Run in loop for multiple countdowns without having to re-click desktop icon. ie Laundry night – 16 minutes to rinse cycle (fabric softener), 13 minutes to load dryer, 58 minutes to take out of dryer.

Edit 5 (Feb 11,2017): Write minutes remaining to /tmp/lock-screen-timer-remaining. This allows other programs to display amount of time before screen is locked.

Edit 6 (Aug 07,2017): Change /tmp/ work file directory to ~/. to support multiple users on network.

Edit 7: REDACTED because ogg123 is no longer used.

Edit 8 (Nov 12,2017): Add WSL (Windows 10 Subsystem for Linux) support. Note you need to add VcXsrv and ubuntu-desktop or xubuntu-desktop (preferred) to use Linux GUI in WSL. Windows 10 system tray / notification area time remaining display requires complementing what indicator-sysmonitor does in Linux. A future powershell.exe script is planned for Windows 10 notification area support.

Edit 9 (Feb 19,2018): Fix animation missing the \ from spinning pizza |,/,─,\ sequence.

Edit 10 (Nov 14,2021): New variable WORK_FILE set to ~/.lock-screen-timer-remaining. If previous timer was run from command line and cancelled with Ctrl+C then $WORK_FILE removed on script startup. The following cosmetic changes were made to this answer:


Top ToS ToC Skip

Use gedit to create script lock-screen-timer

Open the Terminal using Ctrl+Alt+T and type:

gedit lock-screen-timer

Copy and paste code from window below to lock-screen-timer

Toggle back to this screen and copy the following code by highlighting it and pressing Ctrl+C:

#!/bin/bash

# NAME: lock-screen-timer
# PATH: $HOME/bin
# DESC: Lock screen in x minutes
# CALL: Place on Desktop or call from Terminal with "lock-screen-timer 99"
# DATE: Created Nov 19, 2016. Last revision Nov 14, 2021.

# UPDT: Updated to support WSL (Windows Subsystem for Linux)
#       Remove hotplugtv. Replace ogg with paplay.
#       May 30 2018 - Cohesion with multi-timer. New sysmonitor indicator style.
#       Nov 13 2021 - Wrap long lines with \ continuation. Shorten comments.
#       Nov 14 2021 - Remove ~/.lock-screen-timer-remaining on startup.

# NOTE: Time defaults to 30 minutes.
#       If previous version is sleeping it is killed.
#       Zenity is used to pop up entry box to get number of minutes.
#       If zenity is closed with X or Cancel, no screen lock timer is launched.
#       Pending lock warning displayed at set intervals.
#       Write time remaining to ~/.lock-screen-timer-remaining

MINUTES="$1" # Optional parameter 1 when invoked from terminal.

# if no parameters set default MINUTES to 30
if [ $# == 0 ]; then
    MINUTES=30
fi

# WORK_FILE contains number of minutes remaining until lock screen
WORK_FILE=~/.lock-screen-timer-remaining

DEFAULT="$MINUTES" # When looping, minutes count down to zero. 
                   # Save deafult for subsequent timers.

# Check if lock screen timer already running
pID=$(pgrep -f "${0##*/}") # All PIDs matching lock-screen-timer name
PREVIOUS=$(echo "$pID" | grep -v ^"$$") # Strip out this running copy ($$$)
if [ "$PREVIOUS" != "" ]; then
    kill "$PREVIOUS"
    rm "$WORK_FILE"
    zenity --info --title="Lock screen timer already running" \
        --text="Previous lock screen timer has been terminated."
fi

# Running under WSL (Windows Subsystem for Linux)?
if cat /proc/version | grep Microsoft; then
    WSL_running=true
else
    WSL_running=false
fi

# Nov 14 2021 - Remove ~/.lock-screen-timer-remaining on startup. It may
#   still exist if <Ctrl>+C was used to kill last instance launched from
#   terminal
if [ -f "$WORK_FILE" ]; then
    rm "$WORK_FILE"
fi

while true ; do # loop until cancel

    # Get number of minutes until lock from user
    MINUTES=$(zenity --entry --title="Lock screen timer" \
        --text="Set number of minutes until lock" --entry-text="$DEFAULT")

    RESULT=$? # Zenity return code
    if [ $RESULT != 0 ]; then
        break ; # break out of timer lock screen loop and end this script.
    fi

    DEFAULT="$MINUTES" # Save deafult for subsequent timers.
    if [[ $MINUTES == 0 ]] || [[ $MINUTES == "" ]]; then
        break ; # zero minutes considered cancel.
    fi

    # Loop for X minutes, testing each minute for alert message.
    (( ++MINUTES )) 
    while (( --MINUTES > 0 )); do
        case $MINUTES in 1|2|3|5|10|15|30|45|60|120|480|960|1920)
	        notify-send --urgency=critical \
	        --icon=/usr/share/icons/gnome/256x256/status/appointment-soon.png \
	        "Locking screen in ""$MINUTES"" minute(s)." ;
            if [[ $WSL_running == true ]]; then  
                powershell.exe -c '(New-Object Media.SoundPlayer \
                "C:\Windows\Media\notify.wav").PlaySync();'
            else
	           paplay /usr/share/sounds/freedesktop/stereo/complete.oga ;
            fi
	       ;;
        esac;

        # Record number of minutes remaining to file other processes can read.
        echo "Lock screen in: $MINUTES Minutes" > "$WORK_FILE"

        sleep 60

    done

    rm "$WORK_FILE"  # Remove countdown work file

    if [[ $WSL_running == true ]]; then  
        # Call lock screen for Windows 10
        rundll32.exe user32.dll,LockWorkStation
    else
        # Call screen saver lock for Ubuntu versions > 14.04.
        dbus-send --type=method_call --dest=org.gnome.ScreenSaver \
            /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
    fi

done # End of while loop getting minutes to next lock screen

exit 0 # Closed dialog box or "Cancel" selected.

Then toggle back to the empty gedit window and paste the code using Ctrl+V. Save the file and exit the editor back to the command prompt.

Top ToS ToC Skip

Mark lock-screen-timer as an executable

Now we need to make the script executable by typing:

chmod +x lock-screen-timer

Test It!

Before calling the script from the GUI, we’ll call it from the terminal so we can see if any error messages are displayed:

~/lock-screen-timer

You are prompted for the number of minutes:

Lock Screen Timer

Set the desired number of minutes and click OK to start the timer. When there are 15, 10, 5, 3, 2 and 1 minute(s) left a system sound is heard and a message bubble appears advising when the screen will be locked. After the screen is locked you need to enter your password to unlock the screen.

Top ToS ToC Skip

Configure Nautilus to execute bash scripts

Nautilus defines what happens when we double click on an executable script when it’s the files display window or a link on on the desktop. Normal behavior is to edit the script using gedit. We want to change this behavior such that it is executed.

Start Nautilus and navigate to directory containing lock-screen-timer. Left click on it once to give it focus. Hover mouse over top menu bar until β€œFile Edit…” menu appears, use:

  1. Click Edit drop-down menu
  2. Click Properties option
  3. Click Behavior tab
  4. Observe the radio option buttons under Executable Text Files
  5. Check radio button Run executable text files when they are opened

Create desktop shortcut link

From previous section lock-screen-timer still has focus. If not, navigate to the script and left click on it once to give it focus. Then use:

Now you can double click on the desktop shortcut link and the script is run. A dialog box appears to get the number minutes. Two buttons are presented Cancel and OK. If you click the X to close the window it is the same as selecting Cancel.

After the timer is running and you double click on it again the first running copy is β€œkilled”. You can now start a new scren lock countdown or click Cancel for no countdown.

Top ToS ToC Skip

Display Time Remaining in systray / notification area

While lock screen timer is running it records how many minutes are remaining into the file ~/.lock-screen-timer-remaining. You can look at this file with the watch command or display it on Ubuntu’s system tray / application indicator bar as shown at the top of this answer. To display time remaining in the notification area, follow the instructions in this Q&A: Can BASH display in systray as application indicator?.

Top ToS ToC
⇧ How to turn off the USB power to my mouse, when I suspend the notebook? Acquiring pre-releases/betas for unreleased distros for testing  β‡©