Abhishek bhutra
Abhishek bhutra

Reputation: 1422

difference between running shell command directly and from shell script sh file

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

Upvotes: 3

Views: 1673

Answers (1)

Marco Leogrande
Marco Leogrande

Reputation: 8468

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.

Upvotes: 1

Related Questions