How to redirect STDIN of background process?

7,263

main.sh

#!/bin/bash
set -e

if [ ! -p in ]; then
    mkfifo in
fi
tail -f in | java -jar app.jar

Send command to the application with following syntax

echo "command" > /home/user/in
Share:
7,263

Related videos on Youtube

Croll
Author by

Croll

Updated on September 18, 2022

Comments

  • Croll
    Croll over 1 year

    In my Ubuntu, i run java application in background. I use bash script to run it and now it looks like:

    nohup java -jar app.jar &
    exit 0
    

    The problem that i want to be able to write an input string to my application, without making it foreground, from different terminals/sessions. Something like

    echo "mytext" > /appdir/in
    

    How should i change my script?

  • Croll
    Croll about 8 years
    Good try but it does not work (with or without nohup). My application simply not running (not started).
  • Jay jargot
    Jay jargot about 8 years
    @DmitrjA So the application start needs to be troubleshooted? The test section here is a real test. Do you have any error message on the console, in the appilcation log file, in nohup.out? My own app.java read line from STDIN and write it to STDOUT.
  • Croll
    Croll about 8 years
    If i run java -jar app.jar < pipe.in java is not started, and terminal hangs untill Ctrl+Z or Ctrl+C
  • Jay jargot
    Jay jargot about 8 years
    try java -jar app.jar < pipe.in & instead
  • Croll
    Croll about 8 years
    Its same, but without hanging. I don't have application sources, but it's normal command line tool. Tried to use "/proc/PID/fd/0", no success, but i could use it incorrectly.
  • Jay jargot
    Jay jargot about 8 years
    The behavior of you java application is not the same as my simple app.java (on Linux). I could not imagine, at time of writing, what could be the root cause, sorry.
  • Croll
    Croll about 8 years
    It works now, i am not sure what changed, but application ignores any other command except the most first one. I tried adding \n to the end of my "echoes".
  • Jay jargot
    Jay jargot about 8 years
    great feedback !
  • loretoparisi
    loretoparisi almost 7 years
    This is good solution, but I do not get how to get stdout from the app.