Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Here's exactly what I wanted to do: Turn ON Lock (Settings > Brightness and Lock), set the time to wait (Lock screen after:), BUT NOT to lock the screen (e.g. no Password prompt) if I'd only close (and of course open) the lid.

I'm not sure if there's some tweak/hack to be able to do that.

(I'm always walking out of my desk but my machine always does something, so "Do nothing" on lid close, but it's a little annoying to keep typing the password like 4x an hour. I need to have it lid closed. BUT, sometimes I do leave my desk for more than an hour and forgot to Ctrl+Alt+L. I'd like it to lock automatically like after an hour of inactivity. If scripting is the only way to do it, can you please help me where to start? Thanks in advance.)

Ubuntu 12.04.02, LightDM, Gnome Shell

share|improve this question
    
Try running acpi_listen in a terminal, then close your laptop lid and open it after a few seconds. Does this command output anything to you? –  edwin Jun 24 '13 at 3:05
    
I tried it 3 times (close, open), and this is the result: button/lid LID0 00000080 00000024 button/lid LID0 00000080 00000025 button/lid LID0 00000080 00000026 button/lid LID0 00000080 00000027 button/lid LID0 00000080 00000028 button/lid LID0 00000080 00000029 Looks like the number's incremental –  Majal Mirasol Jun 24 '13 at 3:09
    
Do it again but once, I need to check if an odd number implies opened or closed. –  edwin Jun 24 '13 at 3:33
    
button/lid LID0 00000080 0000002e button/lid LID0 00000080 0000002f Now it's Hex :) –  Majal Mirasol Jun 24 '13 at 3:53
    
Ok, write a script with the following: while true ; do grep close /proc/acpi/button/lid/*/state; done, run it with source my-script, then close and open your laptop lid and tell me if there's any output. –  edwin Jun 24 '13 at 4:08

1 Answer 1

up vote 4 down vote accepted

Ok, the information you provided in the comments is enough now.

First, you should install xprintidle (sudo apt-get install xprintidle), because we are going to try and emulate the locking mechanism!

Now, write a script (I've call it screen-locker, notice you don't need to put an extension to your filename):

#!/bin/sh

export DISPLAY=:0.0

if /usr/bin/grep -q close /proc/acpi/button/lid/*/state ; then
  # the lid is closed, so nothing to do
  exit 0
fi

# The lid is open

# the idle time before locking is 60 minutes (60 times 60 times 1000 milliseconds)
lock_time_in_minutes=60

lock_time=$(( ${lock_time_in_minutes} * 60 * 1000 ))

if [ "`/usr/bin/xprintidle`" -ge "$lock_time" ] ; then
  /usr/bin/gnome-screensaver-command -l
fi

Say you save it in a directory called /home/your-username/bin. Make it executable

chmod 755 /home/your-username/bin/screen-locker

now in the terminal run

echo '*/10 * * * * /home/your-username/bin/screen-locker' | crontab

to add this script to your crontab. (With this the script will be executed every 10 minutes.)

Now you can navigate to Brightness & Lock and deactivate the locking because the script will lock your screen if (and only if) the lid is open.

(You can change the 60 to 1 minute for the purpose of a quick test. You wait around 2 minutes with the lid open to see if it locks, then do the same waiting but with the lid closed to see if it does not lock).

share|improve this answer
    
Ok, I think I understand what you meant. –  edwin Jun 24 '13 at 2:34
1  
Thanks man! That idea was great. I edited the file to suit my needs. Below is script I made based on yours, which I then included in my Session Startup to be executed during login. Thanks a lot! Script: screenlock –  Majal Mirasol Jun 24 '13 at 7:55
    
I think that with crontab it is more efficient, also notice your vesion won't check if the lid is open or closed. I have edited my answer to reflect what you wanted to accomplish in your version. –  edwin Jun 24 '13 at 12:45
    
One thing that prevents me from using crontab is that fact that it runs the script every minute. In the version above (currently I'm using a newer one, with my own compiled, idle checker program) the program checks idleness every 10 minutes, without the need to read the hdd, since the whole script is maintained in memory, not exiting. And by the way, I needed to have my screen locked whether the lid is closed or not. So I removed that. Thanks. –  Majal Mirasol Jul 13 '13 at 5:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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