0

How can I use a fifo to redirect stdin for a process?

Using systemd, I am starting a service that will infrequently require commands to be sent via stdin. To overcome this, I have tried the following:

mkfifo -m 600 cmdline
/usr/bin/cat cmdline | /opt/jdk-18/bin/java ...

I am able to run a command using the following.

echo -e "this_is_a_command\n" > cmdline

The problem I am having is that this only seems to work for a single command. After running this command once, all other calls to write into the fifo seem to just hang, and the server never process anything else.

How can I use a fifo to redirect input to stdin of a daemon process?

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • Does this answer your question? [Is a named pipe able to do what i want to do?](https://stackoverflow.com/questions/33093954/is-a-named-pipe-able-to-do-what-i-want-to-do) – stark Apr 03 '22 at 15:33
  • Shouldn't that be `java -jar your-service.jar < cmdline`? It might be better to use a socket – g00se Apr 03 '22 at 17:26
  • @stark - Unfortunately this is a precompiled application, and I do not have access to the source - this is the reason I am trying to do it in this way. – Matt Clark Apr 03 '22 at 17:38
  • @g00se - I tried this method, and bash seems to never even run the _java_ binary, it just hangs on the fifo? – Matt Clark Apr 03 '22 at 17:46
  • What makes you think java is not invoked? The Java class will read the fifo or block if there's nothing to read – g00se Apr 03 '22 at 18:52
  • @g00se With multiple terminals to the same server, when running the command as described in one terminal, the command just hangs forever and I never see the java process start using the other terminal. – Matt Clark Apr 03 '22 at 19:07

1 Answers1

0

Can you try :

tail -f cmdline | /opt/jdk-18/bin/java ...
Philippe
  • 20,025
  • 2
  • 23
  • 32