10

Is there an application for Ubuntu which will lock the computer after a defined amount of time(e.g. 30 minutes). I need it for kids to limit the time they watch cartoons. E.g. 30 minutes watching and then PC locks requiring a parent to unlock it.

For Windows I used Eyes Relax app, it shows black screen and green progress bar(time left to unlock PC) and requires a password to override the lock.

14

June 4, 2018 Update

A much superior program called multi-timer has been created: A timer to set up different alarms simultaneosly

Lock Screen Timer

systray.gif

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

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

  • Use gedit to create script lock-screen-timer
  • Copy and paste code from this window to lock-screen-timer
  • Mark lock-screen-timer as an executable
  • Test it!
  • Configure Nautilus to execute bash scripts
  • Create desktop shortcut link
  • Monitor time remaining

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 to ~/. to support multiple users on network.

EDIT: Note that the script depends on ogg123 to play the reminder sound. This program is part of the vorbis-tool package from the universe repository. The script as presented here also attempts to use the hotplugtv script found here.

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.

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 May 30, 2018.
# UPDT: Updated to support WSL (Windows Subsystem for Linux)
#       Remove hotplugtv. Replace ogg with paplay.
#       Cohesion with multi-timer. New sysmonitor indicator style.

# 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 on-screen 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

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 ~/.lock-screen-timer-remaining
    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


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" > ~/.lock-screen-timer-remaining

        sleep 60

    done

    rm ~/.lock-screen-timer-remaining # Remove work file others can see our progress with

    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.

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.

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:

  • Right click on the file and the context-menu options appear.
  • From the menu select Make Link.
  • A new icon appears called Link to lock-screen-timer.
  • Left click on the new icon and drag it from Nautilus to your desktop.

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.

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?).

  • kids-tv-timer 1 gives kids-tv-timer: command not found. Is there a way to make a shortcut on desktop or in task bar? To just click it and activate the application? – Dmitriy Oct 27 '16 at 10:01
  • the file is there, if I double click it it opens in gedit. I've executed all your commands – Dmitriy Oct 27 '16 at 10:11
  • I changed the last line to ~/kids-tv-timer 1. Sorry the path was missing in front. Try that. – WinEunuuchs2Unix Oct 27 '16 at 10:13
  • yes, that helped. But you didn't answer the 2nd question? :) – Dmitriy Nov 5 '16 at 19:35
  • @geotavros A progress display that sits over the video would require a totally different approach than the sleep command used in kids-tv-timer bash script. It would be possible to create a loop where every minute a notification bubble that lasts for about 10 seconds popped up over the video saying "29 minutes left", then "28 minutes left", etc. However to override you'd still have to interrupt the video, kill kids-tv-timer and restart it with new time allotment. It's almost easier letting the time expire, logging on and restarting kids-tv-timer Let me know. – WinEunuuchs2Unix Nov 5 '16 at 20:00
1

You may want to use something like Mkahawa, which is used to administer internet cafes.

  • This look too complicated. There's server and client. How do I install it? – Dmitriy Oct 27 '16 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.