Prevent Ubuntu from breaking wifi connection on lid close

Asked by Salil Surendran

I have my power settings on Ubuntu to suspend when inactive for 10 minutes. And I have set my 'Power on lid close on battery' as Do Nothing. However, with this setting my laptop doesn't suspend itself if I close my lid but neither does it suspend itself and by the time I get home the laptop is completely dead. If I set my 'Power on lid close on battery' to Suspend then it suspends itself however, it also cuts out all the wifi and VPN connections. When I commute I have to transfer between buses etc. In which case I have to close the lid and my connection to mifi shouldn't break since each time it does I have reconnect to wifi etc. But let's say I reach home and leave my laptop in my bag since it is inactive for 10 minutes I would like to suspend itself so that it doesn't die.

Can this script be used to achieve what I want, i.e. monitor activity but if it is below a threshold for sometime suspend the laptop.

Question information

Language:
English Edit question
Status:
Solved
For:
Keep.Awake Edit question
Assignee:
DanglingPointer Edit question
Last query:
Last reply:
Revision history for this message
DanglingPointer (ferncasado) said :
#1

I have my power settings on Ubuntu to suspend when inactive for 10 minutes.

And I have set my 'Power on lid close on battery' as Do Nothing.

However, with this setting my laptop doesn't suspend itself if I close my lid but neither does it suspend itself and by the time I get home the laptop is completely dead.

If I set my 'Power on lid close on battery' to Suspend then it suspends itself however, it also cuts out all the wifi and VPN connections.

When I commute I have to transfer between buses etc. In which case I have to close the lid and my connection to mifi shouldn't break since each time it does I have reconnect to wifi etc.

But let's say I reach home and leave my laptop in my bag since it is inactive for 10 minutes I would like to suspend itself so that it doesn't die.

Can this script be used to achieve what I want, i.e. monitor activity but if it is below a threshold for sometime suspend the laptop.

-------------------------------------------

Hi Salil,

Assuming you are on Ubuntu 16.04 LTS then I believe it may solve most of your use cases. You will need to test it out to confirm.

As I have described in the Overview; this program will monitor:
1) Network traffic on a specific NIC (only one NIC, it will detect a default but you can change it e.g. your ethernet port or your wireless adapter).
2) User activity in the form of mouse or keyboard input by monitoring the X desktop manager for a duration of idleness.
3) CPU activity monitoring holistic load average of all cores averaged out.

a) For 1-3 above, ALL OF THEM have to be below a threshold for the countdown to begin.
b) There is a grace period before the real countdown begins.
c) Once the grace has past and everything is still below threshold then the program will attempt to use Distribution's built in power-management settings (Ubuntu's Power Management) to set the target suspend date-time.
d) It will continue to monitor and knows when it is meant to suspend.
e) If the target schedule expires and the system is still not suspended, then it will attempt to force suspension by bypassing Ubuntu's Power Management settings and issue a command to the underlying linux-init SystemD (ubuntu 16.04+ or any Distribution using SystemD init system). [For Ubuntu 14.04 or less than ubuntu 16.04, it will attempt to force suspension by making a dbus call].
f) A failed call in step 'e' above will trigger an attempt to try and suspend using an alternative method (either SystemD or dbus).
g) If the alternative fails, then the program exits.

To test and see that it is working for you, run the program with "Verbose" log level of detail. It will print to the terminal and write to a log file at the same time unless you execute it like this
  "$ nohup ./keepawake.py {options go here} > /dev/null 2>&1 &"
in which case all output just goes to the log file.

Let me know how you go.

Revision history for this message
DanglingPointer (ferncasado) said :
#2

Closing this thread as have not heard back. Assuming that he has understood my reply and solved his use-case.

Revision history for this message
DanglingPointer (ferncasado) said :
#3

Use-case query answered