1

I am trying to lock screen in the ubuntu server using "gnome-screensaver-command" and then unlock it by pressing enter key. This is working fine when I am doing it manually and hence thought of automating the same with Python Subprocess module.

The problem I am facing is it is locking the screen and after the timer expiry the enter key("echo -ne \n", got to know this can be used to send enter through CLI) is not happening.

Below is the code snippet.

import subprocess
import time


cmd = subprocess.call(["/usr/bin/gnome-screensaver-command", "-l"])
time.sleep(10)
subprocess.call(["echo", "-ne", "\n"])

Tried another way of doing it:

#******THIS METHOD IS NOT LOCKING THE SCREEN AT ALL*******

cmd = subprocess.Popen(["/usr/bin/gnome-screensaver-command", "-l"], stdin = subprocess.PIPE, shell=True)
time.sleep(10)
cmd.communicate(input="\n")

What should be done to work it as per my requirement ?

Here_2_learn
  • 5,013
  • 15
  • 50
  • 68

1 Answers1

0

the problem is that the screensaver reads the keyboard but not through the process standard input.

Something that (maybe) would work would be to send a keyboard event to the desktop. There are programs (like caffeine on Windows) which do that to prevent the screensaver from activating, but let me propose a portable alternative which doesn't involve keyboard events:

  • run the process
  • wait 10 seconds
  • kill the screensaver process

like this:

import subprocess
import time


cmd = subprocess.Popen(["/usr/bin/gnome-screensaver-command", "-l"])
time.sleep(10)
cmd.terminate()
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219