11

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.

Dmitriy
  • 285
  • Just curious if any of the answers below worked for you? – WinEunuuchs2Unix Oct 17 '16 at 10:58
  • I'll test them when I get the chance and mark the answer – Dmitriy Oct 17 '16 at 15:52
  • I've just discovered similar Questions and Answers that may be of use 1) http://askubuntu.com/questions/715721/how-to-lock-screen-30-minutes-after-unlock/716039#716039 2) http://askubuntu.com/questions/68918/how-do-i-restrict-my-kids-computing-time and 3) http://askubuntu.com/questions/696620/a-timer-that-automatically-locks-the-screen-to-take-a-break-away-from-the-comput – WinEunuuchs2Unix Feb 13 '17 at 00:22

2 Answers2

16

June 4, 2018 Update

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

Lock Screen Timer

systray.gif

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:

  • 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

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:

  • Clean up text
  • Make images in this post smaller by appending m (medium) to their filenames.
  • Add fenced code block: ``` bash - for script syntax highlighting.

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=&quot;Lock screen timer&quot; \
    --text=&quot;Set number of minutes until lock&quot; --entry-text=&quot;$DEFAULT&quot;)

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

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

# Loop for X minutes, testing each minute for alert message.
(( ++MINUTES )) 
while (( --MINUTES &gt; 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 \
        &quot;Locking screen in &quot;&quot;$MINUTES&quot;&quot; minute(s).&quot; ;
        if [[ $WSL_running == true ]]; then  
            powershell.exe -c '(New-Object Media.SoundPlayer \
            &quot;C:\Windows\Media\notify.wav&quot;).PlaySync();'
        else
           paplay /usr/share/sounds/freedesktop/stereo/complete.oga ;
        fi
       ;;
    esac;

    # Record number of minutes remaining to file other processes can read.
    echo &quot;Lock screen in: $MINUTES Minutes&quot; &gt; &quot;$WORK_FILE&quot;

    sleep 60

done

rm &quot;$WORK_FILE&quot;  # 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 &gt; 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 05 '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 05 '16 at 20:00
  • @geotavros The notification bubble every minute might not work when video is in full-screen mode though... – WinEunuuchs2Unix Nov 05 '16 at 20:07
  • I ment this question: Is there a way to make a shortcut on desktop or in task bar? To just click it and activate the application? – Dmitriy Nov 06 '16 at 09:33
  • @geotavros I've added 3 new sections: 1. Make Nautilus execute bash scripts, 2. Create desktop shortcut link and 3. Killing tv-timer running in background. – WinEunuuchs2Unix Nov 06 '16 at 15:00
  • is the "killing" step required? The idea was to make it usable by non techical people – Dmitriy Nov 12 '16 at 15:03
  • @geotavros Final version is now posted. Enhancements include: 1) Auto-delete previous running copy. 2) Dialog box to get number of minutes. 3) Remaining time notification bubbles with system sound at 15, 10, 5, 3, 2 and 1 minutes remaining. 4) Name change to lock-screen-timer. I hope you like it. – WinEunuuchs2Unix Nov 20 '16 at 00:05
  • This script works nicely. Thank you! When I run it in terminal it gives some sort of warning: line 25: [: !=: requires unary operator. One option I'd like is it to run in cycle, so we don't need to click it all the time, for it to automatically lock screen every 30 minutes. – Dmitriy Jan 29 '17 at 17:31
  • You're most welcome. I use the script myself to time my laundry wash-rinse - dry cycles. So I must thank you for requesting it! I'm going out now but I'll look at line 25 tonight. You can setup a script every time Ubuntu's lock screen is unlocked but I'll have to research that. – WinEunuuchs2Unix Jan 29 '17 at 17:41
  • @geotavros I added double quotes around "$PREVIOUS" on line 25 to make error message go away. I've completed R&D for getting lock-screen-timer to relaunch after login but it will take some time to document it above. – WinEunuuchs2Unix Jan 29 '17 at 18:37
  • @geotavros Your last request to run in cycle has been implemented. This took a long time because originally I set this up in dbus-monitor which, although interesting, was overly complicated. The few lines of code added in the second setup follows KISS philosophy (Keep it Simple Stupi_). – WinEunuuchs2Unix Feb 04 '17 at 19:15
  • Nicely done. I edited to include the requirement of ogg123 for the reminder sound. also needs hotplugtv but I'll leave that addition to you as I need some sleep. :-) – Elder Geek Oct 13 '17 at 02:25
  • @ElderGeek Thanks for your help and nice comment :) I'll be updating this for WSL support and Windows Notification Area bubbles hopefully in two weeks and I'll incorporate all your recommendations. This was one of my first bash projects and definitely needs polishing. – WinEunuuchs2Unix Oct 13 '17 at 10:25
  • @ElderGeek hotplugtv seems to be no longer required on mew laptop with nVidia drivers instead of Intel iGPU. Either that or a fresh install of Ubuntu 16.04 vs. upgrade from 14.04 on old laptop fixed pulse audio. Either way I've commented out that code section. – WinEunuuchs2Unix Nov 12 '17 at 17:52
1

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

Byte Commander
  • 107,489