简体   繁体   中英

How to unlock ubuntu Linux using python when screen is locked and hibernated

When my ubuntu locks out due to user locking the system, or due to inactivity, I'am not able to unlock and relogin using python.

The issue is that when my screen locks out, the screen goes black and blank (similar to hibernation but I'am using Desktop PC). The only way I have been able to bypass the hibernation is by pressing spacebar on my keyboard (as you would normally).

I tried to press space using pyautogui but its being ignored and the hibernation persists unless I press the physical keyboard.

I tried the following script which is successful in logging me back in as long as I first override the hibernation by pressing space bar.

import os
import subprocess

subprocess.Popen(['loginctl unlock-session 2'],shell=True) #2 is my session id

Possible solution routes would be:

  • To avoid hybernation altogether
  • to bypass the hibernation using python script such that I can run the above code after to unlock/re-login.

Looking forward to your proposals. Much appreciated

You can use dbus to get gnome screensaver and disable it
Install dbus pip install dbus-python

import dbus
sessionBus = dbus.SessionBus()
screenSaver = sessionBus.get_object("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver")
screenSaverIface = dbus.Interface(screenSaver, 'org.gnome.ScreenSaver')
screenSaverSetActive = screenSaverIface.get_dbus_method("SetActive")
screenSaverSetActive(False)

source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM