Bash – How to Redirect STDIN of Background Process

bashioio-redirectionnohupstdin

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?

Best Answer

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 Question