Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 821
Abhishek bhutra Avatar asked Mar 06 '26 02:03

Abhishek bhutra


1 Answers

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.
like image 171
Marco Leogrande Avatar answered Mar 07 '26 15:03

Marco Leogrande



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!