Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved] HowTo Configure laptop to sleep when lid is closed
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Tue Sep 30, 2014 12:56 am    Post subject: [Solved] HowTo Configure laptop to sleep when lid is closed Reply with quote

I was wondering if anyone can help me create a script for sleeping and invoking it when the lid of a laptop is closed.
This is what I have so far, but when I close the lid, it doesn't work :/
Code:
$acpi_listen
button/lid LID close
button/lid LID open
Code:
cat /etc/acpi/events/lm_lid
event=button/lid
action=/etc/acpi/actions/lm_lid.sh %e
Code:
cat /etc/acpi/actions/lm_lid.sh
button/lid
    case "$2" in
        SLPB) sync; echo -n mem >/sys/power/state ;;
    *)    logger "ACPI action undefined: $2" ;;
    esac
    ;;


Last edited by ShanaXXII on Sat Oct 04, 2014 11:55 pm; edited 1 time in total
Back to top
View user's profile Send private message
Roman_Gruber
Advocate
Advocate


Joined: 03 Oct 2006
Posts: 3846
Location: Austro Bavaria

PostPosted: Tue Sep 30, 2014 9:03 am    Post subject: Reply with quote

Why don t you use tuxonice sources or something like that?

this used to work for me years ago quite well.

or you use a desctop environment which comes with that support out of the box. kde and gnome used to have such features afaik.

assuming you use already a de, why dont you search if it already exists there this feature?

if not tuxonice could be a good starting point
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Tue Sep 30, 2014 11:32 am    Post subject: Reply with quote

tw04l124 wrote:
Why don t you use tuxonice sources or something like that?

this used to work for me years ago quite well.

or you use a desctop environment which comes with that support out of the box. kde and gnome used to have such features afaik.

assuming you use already a de, why dont you search if it already exists there this feature?

if not tuxonice could be a good starting point

I'm on a windows manager so it doesn't really work for me.
and I don't really want to use tuxonices but i'll try it and see
Back to top
View user's profile Send private message
albright
Advocate
Advocate


Joined: 16 Nov 2003
Posts: 2588
Location: Near Toronto

PostPosted: Tue Sep 30, 2014 11:50 am    Post subject: Reply with quote

Just to be sure, I take it that

Quote:
echo -n mem >/sys/power/state


puts the machine to sleep if run manually?


So, why do you have SLBP instead of "close" in your
script?
_________________
.... there is nothing - absolutely nothing - half so much worth
doing as simply messing about with Linux ...
(apologies to Kenneth Graeme)
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Tue Sep 30, 2014 11:54 am    Post subject: Reply with quote

You don't need tuxonice for that.

You are getting the events right, ok? Then all that fails is the mechanisms that ties actions to these events.

Things to take into account:

  • acpid needs to be running
  • acpid, for some reason, needs to be restarted each time you change anything into /etc/acpid/*, so, each time you add, delete or edit a file in there do "/etc/init.d/acpid restart". I am not 100% sure if this applies only to newly created files or it happens also for modified ones. Maybe it only needs a restart when you create an event. So you'll have to check yourself.
  • not sure, but common sense suggests that everything under /etc/acpid/actions/ need to be chmod'ed a+rx so the scripts will run


Besides that, there's an error in your scripting.

Code:

button/lid
    case "$2" in
        SLPB) sync; echo -n mem >/sys/power/state ;;
    *)    logger "ACPI action undefined: $2" ;;
    esac
    ;;


I am not sure if this is parsed by bash or whatever /bin/sh is pointing at. I would just stick to plain POSIX sh, but that's just me. In any case, note that "button/lid" is probably not a valid sentence in any shell that I know of, that is, unless there's a "button/lid" executable that can be found starting from the current working directory.

You could test with something simple, such as this, then once that works elaborate from there:

lm_lid.sh wrote:

echo "$(date) $@" >> /foo.txt


That should add a line into a file called /foo.txt each time you open or close the lid, with the date and the event that was received. If that works, the rest is just shell scripting. :)
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Wed Oct 01, 2014 12:42 am    Post subject: Reply with quote

albright wrote:
Just to be sure, I take it that

Quote:
echo -n mem >/sys/power/state


puts the machine to sleep if run manually?


So, why do you have SLBP instead of "close" in your
script?

I'm not sure, thats why i need help. :oops:
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Wed Oct 01, 2014 12:50 am    Post subject: Reply with quote

i92guboj wrote:
You don't need tuxonice for that.

You are getting the events right, ok? Then all that fails is the mechanisms that ties actions to these events.

Things to take into account:

  • acpid needs to be running
  • acpid, for some reason, needs to be restarted each time you change anything into /etc/acpid/*, so, each time you add, delete or edit a file in there do "/etc/init.d/acpid restart". I am not 100% sure if this applies only to newly created files or it happens also for modified ones. Maybe it only needs a restart when you create an event. So you'll have to check yourself.
  • not sure, but common sense suggests that everything under /etc/acpid/actions/ need to be chmod'ed a+rx so the scripts will run


Besides that, there's an error in your scripting.

Code:

button/lid
    case "$2" in
        SLPB) sync; echo -n mem >/sys/power/state ;;
    *)    logger "ACPI action undefined: $2" ;;
    esac
    ;;


I am not sure if this is parsed by bash or whatever /bin/sh is pointing at. I would just stick to plain POSIX sh, but that's just me. In any case, note that "button/lid" is probably not a valid sentence in any shell that I know of, that is, unless there's a "button/lid" executable that can be found starting from the current working directory.

You could test with something simple, such as this, then once that works elaborate from there:

lm_lid.sh wrote:

echo "$(date) $@" >> /foo.txt


That should add a line into a file called /foo.txt each time you open or close the lid, with the date and the event that was received. If that works, the rest is just shell scripting. :)


I edited my lm_lid.sh with only echo "$(date) $@" >> /foo.txt
Restarted acpid and it wouldn't execute the script when i close the lid
:/
Back to top
View user's profile Send private message
albright
Advocate
Advocate


Joined: 16 Nov 2003
Posts: 2588
Location: Near Toronto

PostPosted: Wed Oct 01, 2014 1:59 am    Post subject: Reply with quote

Quote:
I'm not sure, thats why i need help.


OK, well, open a terminal and run as root

Code:
echo -n mem >/sys/power/state


your machine should go to sleep; closing the lid and
re-opening *should* waken it or a touch of the
power button ...
_________________
.... there is nothing - absolutely nothing - half so much worth
doing as simply messing about with Linux ...
(apologies to Kenneth Graeme)
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Wed Oct 01, 2014 5:33 am    Post subject: Reply with quote

If the scipt I posted doesn't run then hibernation is n9t the problem. Is it marked executable for all users?
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Wed Oct 01, 2014 6:35 am    Post subject: Reply with quote

ShanaXXII, I (and probably everyone else around here) can help you with the scripting part, but that will come later.

The real milestone is getting a basic script to work. If the script is never launched then it's not worth the effort of putting more complex stuff into it. First thing first. :)
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Fri Oct 03, 2014 12:20 pm    Post subject: Reply with quote

i92guboj wrote:
If the scipt I posted doesn't run then hibernation is n9t the problem. Is it marked executable for all users?

Terribly sorry for the long reply. It was actually working, i just didn't find the foo.txt. Sorry ):

If I make it echo "mem" > /sys/class/state will I need root-privileges for it to work?
Back to top
View user's profile Send private message
albright
Advocate
Advocate


Joined: 16 Nov 2003
Posts: 2588
Location: Near Toronto

PostPosted: Fri Oct 03, 2014 12:23 pm    Post subject: Reply with quote

Quote:
If I make it echo "mem" > /sys/class/state will I need root-privileges for it to work?


yes indeed
_________________
.... there is nothing - absolutely nothing - half so much worth
doing as simply messing about with Linux ...
(apologies to Kenneth Graeme)
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Fri Oct 03, 2014 12:36 pm    Post subject: Reply with quote

Just to clarify, the script will always run as root (or whatever privileges the acpi daemon grants it) each time the lid is closed. You don't need to be loged in as root or something like that.

Just give that script the same permissions that the rest of the acpi scripts under /etc/acpid/actions have.
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Sat Oct 04, 2014 11:17 am    Post subject: Reply with quote

i92guboj wrote:
Just to clarify, the script will always run as root (or whatever privileges the acpi daemon grants it) each time the lid is closed. You don't need to be loged in as root or something like that.

Just give that script the same permissions that the rest of the acpi scripts under /etc/acpid/actions have.

Code:
cat /etc/acpi/actions/lm_lid.sh
echo "mem" > /sys/class/state

But when I close my lid, nothing happens :/
Back to top
View user's profile Send private message
albright
Advocate
Advocate


Joined: 16 Nov 2003
Posts: 2588
Location: Near Toronto

PostPosted: Sat Oct 04, 2014 12:26 pm    Post subject: Reply with quote

just to satisfy my curiousity and for total clarity what happens if you run

Quote:
echo -n mem >/sys/power/state


from a terminal as root; does the machine go to sleep?
_________________
.... there is nothing - absolutely nothing - half so much worth
doing as simply messing about with Linux ...
(apologies to Kenneth Graeme)
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Sat Oct 04, 2014 3:28 pm    Post subject: Reply with quote

ShanaXXII wrote:
i92guboj wrote:
Just to clarify, the script will always run as root (or whatever privileges the acpi daemon grants it) each time the lid is closed. You don't need to be loged in as root or something like that.

Just give that script the same permissions that the rest of the acpi scripts under /etc/acpid/actions have.

Code:
cat /etc/acpi/actions/lm_lid.sh
echo "mem" > /sys/class/state

But when I close my lid, nothing happens :/


Try /sys/power/state instead.

If that fails, please, run this in an xterm (as root), then close the lid, open again and post the output:

Code:
tail -f /var/log/messages
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Sat Oct 04, 2014 9:08 pm    Post subject: Reply with quote

i92guboj wrote:
Try /sys/power/state instead.

I'm so stupid. I should've seen that I messed up the path to the file. T^T
It is working now.
There is one problem that sprung up though.
When I close the lid, it goes to sleep, when i open it, it wakes up then goes to sleep again.
Any suggestions on how to fix this? :/
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Sat Oct 04, 2014 9:30 pm    Post subject: Reply with quote

Oh, I didn't think that you were including only that line in the script.

Let me explain a bit how it works so you get at least the general idea.

The acpid daemon listen for acpi events, then launches the associated scripts according to the files in /etc/acpi/events/*, in this case, you have associated the "button/lid" event to lm_lid.sh, so, each time such event is triggered the script is run, without discriminating if you are opening or closing the lid.

So, it's up to the script to parse that. You can do that using a case statement, such as in your first example, or some other scripting structure. You should also use $3 instead of $2 (read about "positional parameter in the bash man page). That variable will hold either "close" or "open" when used in your script.

Example:

Code:

if [[ "$3" == "close" ]]
then
  echo "mem" > /sys/power/state
else if [[ "$3" == "open" ]]
then
  /etc/init.d/connman restart
fi


This will put to sleep the box if you "close" the lid, and restart the network daemon when you "open" it. Of course it's just an example. You might use a different network daemon that handles suspend right :lol:

EDIT: if you need further explanation just ask. I don't want to spoil the fun of it :)


Last edited by i92guboj on Sat Oct 04, 2014 11:12 pm; edited 1 time in total
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Sat Oct 04, 2014 10:50 pm    Post subject: Reply with quote

i92guboj wrote:
Oh, I didn't think that you were including only that line in the script.

Let me explain a bit how it works so you get at least the general idea.

The acpid daemon listen for acpi events, then launches the associated scripts according to the files in /etc/acpi/events/*, in this case, you have associated the "button/lid" event to lm_lid.sh, so, each time such event is triggered the script is run, without discriminating if you are opening or closing the lid.

So, it's up to the script to parse that. You can do that using a case statement, such as in your first example, or some other scripting structure. You should also use $3 instead of $2 (read about "positional parameter in the bash man page). That variable will hold either "close" or "open" when used in your script.

Example:

Code:

if [[ "$3" == "close" ]]
then
  echo "mem" > /sys/power/state
else if
  /etc/init.d/connman restart
fi


This will put to sleep the box if you "close" the lid, and restart the network daemon when you "open" it. Of course it's just an example. You might use a different network daemon that handles suspend right :lol:

EDIT: if you need further explanation just ask. I don't want to spoil the fun of it :)

I'm not really understanding the script and the script doesn't seem to work.
Also, I don't think I have a /etc/init.d/connman.
Could you explain how this script works?
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Sat Oct 04, 2014 11:22 pm    Post subject: Reply with quote

I corrected the scrip. I got interrupted while writing and it was a bit messed. Dunno if my phone is to blame or if it was me :p

As for the script, well, it is invoked by the acpi daemon, as said. Do you see that "%e" in the event line in your first post? That means "pass the whole acpi_listen output to lm_lid.sh".

So, lm_lid.sh receives a line like this: "button/lid LID close ". In this line, "$3" is the third element, that is "close".

The script says this: if the third element in this line is close then put a value into /sys/power/state, else, if it's open restart my wifi daemon".

And, as said in the other post, that's just an example. I wanted to ilustrate how to handle the "open lid case". You can remove the "else if...then" and "connman" lines. Be sure to keep the "fi", which closes the if sentence.
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Sat Oct 04, 2014 11:26 pm    Post subject: Reply with quote

Code:

if [[ "$3" == "close" ]]
then
  echo "mem" > /sys/power/state
fi
Back to top
View user's profile Send private message
ShanaXXII
Apprentice
Apprentice


Joined: 29 Jun 2014
Posts: 283
Location: Canada

PostPosted: Sat Oct 04, 2014 11:55 pm    Post subject: Reply with quote

i92guboj wrote:
Code:

if [[ "$3" == "close" ]]
then
  echo "mem" > /sys/power/state
fi

Thank you so much (:
This has been a great learning experience and I appreciate it alot (:
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum