0

I've a service written in Python that can receive commands and does things, here is how it is built:

    print("\nWelcome!\n")
for line in sys.stdin:
    cmd=line.rstrip()
    if cmd == "ls" or cmd == "help":
        print("Available Commands:")

Now this service is started with systemd and its unit file has the following:

[Service]
Environment=PYTHONUNBUFFERED=1
ExecStart=...service.py
WorkingDirectory=...

StandardOutput=tty StandardInput=tty-force TTYVHangup=yes TTYPath=/dev/tty60 TTYReset=yes

So the service input and output is attached to the tty60. I can use conspy 60 to see what the service is doing and interact with it. For instance here I typed test and then help:

test
Error: command 'test' not found.
Type 'help' for a list of available commands.

help Avaliable Commands: > status, fan1, fan2, fan3, fan4, wake, sleep, quit

Python is able to grab what I typed and answers as expected.

Now, how can I send a command to the tty without entering an interactive program like conspy?

I tired echo -e "ls\n" >> /dev/tty60 and in another terminal with conspy I can see the command but Python didn't process it:

help **---> sent via conspy**
Avaliable Commands:
> status, fan1, fan2, fan3, fan4, wake, sleep, quit
help **---> sent with "echo"**

As you can see the last help sent via echo wasn't processed.

Thank you.

TCB13
  • 741
  • The usual way to receive commands is to create a listening socket, not to hijack a tty device. It works, but it's not really "best practice" – Chris Davies Mar 19 '22 at 23:40
  • Yeah but I need to be able to interact with it directly via a tty / cli but also have a script sending commands on timers etc. – TCB13 Mar 19 '22 at 23:56
  • 1
    Yes, sending output to a tty does not nagicially make it input from that tty. On Linux (and some other Unices but not all) to fake tty input use ioctl(fd,TIOCSTI,ptr) like conspy does; putting TIOCSTI in the search box at the top of this page finds dozens more examples. – dave_thompson_085 Mar 20 '22 at 00:58
  • @dave_thompson_085 I ended up going with https://unix.stackexchange.com/a/570854/23085. Thank you for the explanation of the issue, if you post it as an answer I'll accept it. – TCB13 Mar 20 '22 at 10:50

0 Answers0