2

I have an application which I can run from the commonand line and it acts like a CLI for me.

However its part of a suite of applications and now I want them all to run on boot up and so they are being started as a service (systemd).

So all my apps includeing my CLI are running great. However I can't see the output of the CLI and I can't send commands to it.

I was using the stdout to print the CLI screen - I guess I can work around this by outputting to a file - but it would be nice to get access to its stdout. The bigger issue is that I can't write to its stdin.

I read this: writing-to-stdin-of-background-process and tried to echo -e "command\n" > /proc/.../0, but nothing happened - I checked the log of my CLI (gets written to a file) and it did not get the input.

I then did a ls -l on /proc/<pid>/fd/ and I notice that 0 --> /dev/null meaning stdin is linked to /dev/null.

So, how can I get access to its stdin?

Bonus question - is there a way to operate (in the same bash shell) such that I re-direct my stdin to the stdin of this process AND redirect the process stdout to my shell?

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • I don't know if it works, but one idea is to create fifo using `mkfifo /tmp/hoo`. And in the systemd file set: `ExecStart=/bin/sh -c '/your/cli/command < /tmp/hoo'`. Now if you `echo -e "command\n" > /tmp/hoo"` it should be piped to `/your/cli/command` – ymonad Apr 11 '19 at 08:23
  • @ymonad I am just testing this on the command line first. When I run `./myapp < /tmp/hoo` (after making the fifo) it does not seem to run the app until I do my first echo command to it. That's quite strange! - but it does start to work after that – code_fodder Apr 11 '19 at 08:41
  • maybe your cli command is not intended to access via stdin. did you checked that `echo command | /your/command` works? – ymonad Apr 11 '19 at 09:19
  • @ymonad - yes that worked... Your first idea did work, but the app did not seem to start until the received the first command - and then it ran normally – code_fodder Apr 11 '19 at 09:37

1 Answers1

2

Find the terminal (pty) that You want to redirect. Example for pty0 redirection can be done via:

exec < /dev/pty0  #stdin
exec > /dev/pty0  #stdout
Kubator
  • 1,373
  • 4
  • 13
  • How do you find the /dev/pty? – code_fodder Apr 11 '19 at 08:54
  • Similar You did for background process /proc//fd/. So go to active terminal , then do echo $$, that will gives You pid of current shell and if shell is interactive it must have set pty in /proc//fd/ – Kubator Apr 11 '19 at 08:58