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
Related videos on Youtube
Author by
Croll
Updated on September 18, 2022Comments
-
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 about 8 yearsGood try but it does not work (with or without nohup). My application simply not running (not started).
-
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 about 8 yearsIf i run
java -jar app.jar < pipe.in
java is not started, and terminal hangs untill Ctrl+Z or Ctrl+C -
Jay jargot about 8 yearstry java -jar app.jar < pipe.in & instead
-
Croll about 8 yearsIts 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 about 8 yearsThe 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 about 8 yearsIt 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 about 8 yearsgreat feedback !
-
loretoparisi almost 7 yearsThis is good solution, but I do not get how to get
stdout
from the app.