3

I have a java program in which I am reading from stdin

BufferedInputStream bis = new BufferedInputStream(System.in);
byte[] b = new byte[1];
int cmd = bis.read(b);
System.out.println("Read command: " + new String(b));

And a shell script to start-stop this program

'start')
if [ -p myfifo ]; then
    rm myfifo
    rm myfifo-cat-pid
fi
mkfifo myfifo
cat > myfifo &
echo $! > myfifo-cat-pid
java -jar lib/myJar.jar >/dev/null 2>&1 0<myfifo &
echo `date +%D-%T` $! >> process.pid
echo "Started process: "$!
;;

'stop')
echo 0 > myfifo
echo "Stopped process: "
rm myfifo
;;

When I run commands in start one by one the program waits until i echo on fifo. But when I run it from .sh file it immediately reads from stdin. Dont understand what is the difference between if run a command directly on command prompt and if I make a .sh file and run it then

1
  • What is read from stdin if you run the .sh file? What is output on screen? Commented Jul 30, 2012 at 7:09

1 Answer 1

1

The difference is not on the Java side, but instead on the fact that your shell handles differently the job control when launching a script. From man bash:

JOB CONTROL
   Job  control  refers  to  the ability to selectively stop (suspend) the
   execution of processes and continue (resume) their execution at a later
   point.   A  user  typically  employs  this  facility via an interactive
   interface supplied jointly by the operating  system  kernel's  terminal
   driver and bash.

As explained here, by default job control is disabled in a script.

When cat > myfifo & is executed in an interactive shell, it remains in "Stopped" mode waiting to be put in foreground again (with fg). When launched in a script, instead, job control is disabled so, as soon as cat tries to read from the (detached) terminal, it exists, closing the pipe (and your Java process reads EOF).

If you use set -m at the top of your shell script (hence enabling forcefully job control), you should see a consistent behavior.

set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
  -m      Monitor mode.  Job control is enabled.  This option is on by
          default for interactive shells on systems  that  support  it
          (see JOB CONTROL above).  Background processes run in a sep‐
          arate process group and a line containing their exit  status
          is printed upon their completion.
Sign up to request clarification or add additional context in comments.

4 Comments

where to use set -m i mean in header??
Before you launch any command with &; that is, probably it's best at the beginning of the script.
i have added first command as set -m... it is making no difference :(
Which shell are you using? If it's not Bash, then probably you shell does not support set -m.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.