LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Lock desktop programmatically (https://www.linuxquestions.org/questions/linux-software-2/lock-desktop-programmatically-738617/)

jlinkels 07-08-2009 10:02 AM

Lock desktop programmatically
 
Our little son is quickly becoming a computer addict, and that interferes with his obligatory piano lessons. In other words, as long as he has access to his computer the piana is gathering dust.

I would like to lock his GUI desktop by means of a program called from cron, or activated thru SSH if I have to do it on unscheduled moments.

I know that KDE has an option to lock the desktop, but then it can be unlocked with the normal user password, which isn't good either.

A more rigorous approach would be to close down the complete computer, but how do I then prevent it from starting again? I could disable his password on the server of the NIS domain to prevent him to login again, but I don't think this is a very elegant solution.

Any ideas? I speak Bash quite good and furthermore C, PHP and TCL/Tk, but no C++ or QT.

jlinkels

colucix 07-08-2009 12:15 PM

Here is my idea: put a script in root's crontab which kill the X session and lock the user, so that he cannot login back. Here is an example of what the script could be (assuming gnome as desktop manager but you can find similar commands for KDE):
Code:

#!/bin/bash
trap : HUP
count=10
( for i in $(seq 10 10 100)
do
  echo "# Closing in $((count--)) seconds"
  sleep 1
  echo $i
done ) | /usr/bin/zenity --auto-close --progress

/usr/bin/pkill -9 gnome-session
/usr/sbin/usermod -L username

This uses zenity to display a message and leave a bunch of seconds, so that your son can save any open file before leaving. Note the trap statement which is useful if the user click on the cancel button in the zenity dialog (which sends an hang-up signal and would stop the script execution).

Then you have to run a cron job as root, using the X display:
Code:

30 19 * * * env DISPLAY=:0 /root/script.sh
this assumes the current DISPLAY is :0. If not, you can always use the command "who" to retrieve the correct DISPLAY inside the script itself.

A final step is to let root uses the X environment of another user. You can do that permanently by editing/creating the file /etc/X11/xinit/xinitrc.d/localuser.sh:
Code:

#!/bin/sh
[ -x /usr/bin/xhost ] && [ -x /usr/bin/id ] &&
    xhost +si:localuser:`id -un` &&
    xhost +si:localuser:root

The last command gives permissions to use any display to the root user.

Note that you have to unlock the user at some time, using another cron job to let your son login again later.

jlinkels 07-08-2009 05:37 PM

Colucix, quite impressive. You put at least three items in that post I have never seen before.

I am going to try something according to these directions, many thanks.

jlinkels


All times are GMT -5. The time now is 01:19 AM.