M
M
Matvey Kot2021-12-18 14:02:43
Python
Matvey Kot, 2021-12-18 14:02:43

How to enter a command in screen in python?

In general, the essence of the program is as follows:

  • The program opens a window (writes to the console: screen -r name).
  • Syncup is written in this window (not to the console, but to the application, because the Java application with log4j is open in the window)
  • Exit the window by "pressing" Ctrl+A+D

At first I thought to enter the window through os / subprocess, write syncup through the keyboard and exit the window using the same keyboard. But the code stupidly opened the window and that's it (even in asynchronous it did just that).
Here is my code:
import subprocess
import keyboard
import asyncio


async def go_screen():
  subprocess.check_call(['screen', '-r', 'name'])


async def lets_keyboard():
  await asyncio.sleep(2.0)
  keyboard.send('s')
  keyboard.send("y")
  keyboard.send("n")
  keyboard.send("c")
  keyboard.send("u")
  keyboard.send("p")
  keyboard.send("ctrl+a+d")

loop = asyncio.get_event_loop()
coros = [go_screen(), lets_keyboard()]
loop.run_until_complete(asyncio.gather(*coros))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-12-18
@MatweyCAT

Have you read the documentation at all? Or laziness?


subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)
Run command with arguments. Wait for command to complete . If the return code was zero then return,

check_call() causes the script to wait until screen -r exits, and screen -r does not exit until it receives Ctrl-A,D.

M
misha356, 2021-12-18
@misha356

Not an answer, but a correction in your code:

keyboard.send('s')
keyboard.send("y")
keyboard.send("n")
keyboard.send("c")
keyboard.send("u")
keyboard.send("p")

Not effective
Effective way: After delay = write your interval between clicks
keyboard.write('syncup', delay=0.1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question