3

I have an application that writes and reads to an interactive terminal, and when I run it via the shell, it works as expected. However, when I run it as a systemd service, it seems as though the application isn't capturing the stdout or stderr.

I have tried modifying the StandardOutput and StandardError in the service definition to journal+console and tty.

The service section of my systemd file looks like this :

[Service] 
ExecStart=/bin/bash -c "cd /somedir && python3 myapp.py" 
ExecStop= 
Restart=always 
RestartSec=10
StandardOutput=tty 
StandardError=tty

I have also tried setting TTYPath=/dev/tty2, but no luck.

I am running Ubuntu 16.04.

Edit: More info

When I run this app, the interactive session that it creates does not get read from/written to in the terminal where I actually run the app. It emulates its own terminal.

See link here:

http://docs.paramiko.org/en/2.4/api/channel.html#paramiko.channel.Channel.get_pty

and here:

http://docs.paramiko.org/en/2.4/api/channel.html#paramiko.channel.Channel.invoke_shell

aleksk
  • 131
  • 1
  • 4
  • 1
    What does systemctl status -l (servicename) show? – ErikF Feb 11 '18 at 06:18
  • @ErikF it shows that it's running, are you looking for something specific? – aleksk Feb 11 '18 at 07:54
  • I wanted to know whether or not the service had died, so that's helpful. You indicated that the program reads input: does this program require input to start working? If so, are you supplying that input from somewhere? – ErikF Feb 11 '18 at 08:09
  • Maybe you want to first test the tty output with something like ExecStart=/bin/echo "Hello" to exclude problems with the myapp.py application. Just tested on a Ubuntu 16.04 VM and TTYPath=tty2. Saw that the getty@tty2 service is only activated when you switch to it. So defined a dependency Requires=getty@tty2.service, now it seems to work but the first output of executing the service is not logged. – Thomas Feb 11 '18 at 08:59
  • if you want to run a different service on tty2, I think you would want to mask getty@tty2 (or maybe autovt@tty2, which is how it is triggered on VT switch). Typically getty services will be triggered on tty1 through tty6. See docs for logind.conf – sourcejedi Feb 11 '18 at 09:49
  • @ErikF No, the program doesn't require input to start working. It uses the python paramiko library which allows you to emulate the terminal in python. Once the program is started, it gets a trigger to read/write to an interactive terminal session via a web request. I can run the script without it being a daemon with no problems. – aleksk Feb 11 '18 at 13:42
  • @Thomas thanks, I tried your suggestion. This didn't work for me - the app is not capturing the stdout and stderr correctly. Unfortunately I don't have the ability to switch to tty2 as I only have access to the server via SSH (and therefore can't test a simple echo). Is there another way I can test whether the echo works? Sorry if this is a stupid question, but is there a way I can make it read/write to a 'virtual tty'? Ideally in that tty I would like to be able to issue git commands for example, via my app. – aleksk Feb 11 '18 at 13:53
  • Added more info to question – aleksk Feb 11 '18 at 14:05
  • 1
    This answer is relevant: https://unix.stackexchange.com/questions/339638/difference-between-systemd-and-terminal-starting-program/339645 . It seems as though points 3 and 4 of the answer are relevant to this situation. – aleksk Feb 12 '18 at 22:14

0 Answers0