Programming and Scripting :: Laptop lid script



You need to replace /proc/acpi/button/lid/C162/state to fit your own laptop. But it's somewhere in the /proc/acpi directory.

Code Sample
#!/bin/bash

LIDSTATE="open"

while true
do
 sleep 1s
 if grep open /proc/acpi/button/lid/C162/state >> /dev/null && [ "$LIDSTATE" == "closed" ]; then
   LIDSTATE="open"
   # Run commands to set you as 'Online'.
 elif grep closed /proc/acpi/button/lid/C162/state >> /dev/null; then
   LIDSTATE="closed"
   # Run commands to set you as 'away'.
  fi
done


You may get the point...
This is really nice script to command your IRC client to set you as away when you close the lid. Also for ICQ and M$N.
But personally I'd put a hd spindown command there (as last command when closing lid). ;) With few secs of delay...

Basically it checks every second if the state of your lid has changed and if it has then it performs commands you have entered.

Maybe 'source /etc/online.run' and '/etc/source away.run' or something like that would be nicer. Then you wouldn't need to edit script itself anymore...

Alternatively, you could load acpid to detect the lid state for your machine automatically and run the script of your choice
As usual, I can't help but pick at little optimization opportunities.
Is it necessary to check the "state" file _AND_ set a state variable explicitly?
To reduce redundancy and filesize, you can check the state file once and determine your action from that.
If you have only two possible options, "elif" is unnecessary

This is untested, since i don't have/use acpid or a laptop =o)
The LIDSTATE especially may not work, since i have no idea what the state file looks like.
Code Sample
#!/bin/bash

while true
do
sleep 1s
LIDSTATE=`cat /proc/acpi/button/lid/C162/state`
if  [ "$LIDSTATE" == "closed" ]; then
  # Run commands to set you as 'away'.
else
  # Run commands to set you as 'Online'.
 fi
done

Also seems to me that one second is a pretty high frequency. Raising that number would be easier on your processor. It's just an opinion, though.

This is why:
Code Sample
$ cat /proc/acpi/button/lid/C162/state
state:      open


And I need to set the variable because that script would then perform commands each second when lid is closed/open. I need to set that variable to tell script that it has already performed those tasks on open/close and won't "reperform" those commands anymore until state has changed.

Every second... Hm. It could be like every 10 seconds. That's true.

I see...hadn't thought about rerunning the same commands for no reason (this thread was the first thinking I did today =o))...good point.

It still seems to me that grepping the state file more than once in a loop may be unnecessary, but it's a lot better compared to what I was thinking this morning.

Next Page...
original here.