3

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?

2 Answers 2

2

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
1
  • 1
    This is good solution, but I do not get how to get stdout from the app. Commented Jun 30, 2017 at 15:21
0

Give this a try:

mkfifo /appdir/in
nohup java -jar app.jar < /appdir/in &
exit 0

The test:

$ ./startapp.sh
$ printf "%s\n" "mytext" >> /appdir/in
$ cat nohup.out
mytext

$

There is additional control to add in order to manage the lifecycle of the /appdir/in named pipe.

7
  • Good try but it does not work (with or without nohup). My application simply not running (not started). Commented Mar 31, 2016 at 11:48
  • @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. Commented Mar 31, 2016 at 11:50
  • If i run java -jar app.jar < pipe.in java is not started, and terminal hangs untill Ctrl+Z or Ctrl+C Commented Mar 31, 2016 at 11:56
  • try java -jar app.jar < pipe.in & instead Commented Mar 31, 2016 at 12:00
  • 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. Commented Mar 31, 2016 at 12:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.