-1

I've prepared a shell script to run a Java file. I've given Java path, class path and dependencies. I'm executing the file based on a condition.

Following is the code snippet. The code gets executing, but the control is not coming out of the prompt.

CMD=""$JRE_BIN_PATH/java" $SP -cp $CP org.aditya.test.MainClass "$@""

if [ "$1" == 'server' ]   && [ "$2" == 'start' ]
then
$CMD &
elif [ "$1" == 'process' ]
then
$CMD &
else
$CMD
fi

Command hangs when if and elif executes, since it is using '&' at the end. But if the control goes to else (without '&') control coming out immediately. I've removed '&' and everything works fine. My question here is, what is the use of '&' at the end of CMD. Why it is causing the problem.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Aditya
  • 115
  • 1
  • 9
  • `CMD=""$JRE_BIN_PATH/java" $SP -cp $CP org.aditya.test.MainClass "$@""` - quoting in the shell doesn't work like you seem to think it does. Those extraneous double-quotes aren't making it into the value of CMD. – davmac Jul 29 '16 at 13:12
  • 2
    Possible duplicate of [In bash, why do shell commands ignore quotes in arguments when the arguments are passed to them as a variable?](http://stackoverflow.com/questions/12136948/in-bash-why-do-shell-commands-ignore-quotes-in-arguments-when-the-arguments-are) – tripleee Aug 01 '16 at 05:56
  • Also http://mywiki.wooledge.org/BashFAQ/050 – tripleee Aug 01 '16 at 05:57

1 Answers1

1

Basically, & is the Linux shell marker for "run in the background". The task will immediately fork and then return. It will not access the standard input and output of the parent process.

You should never use & for tasks that require user input. & should be reserved for background tasks such as services or standalone maintainment tasks (note that under standard behavior, unless you use a tool such as screen, a background task might still terminate when the parent process that backgrounded it terminates.

Piotr Wilkin
  • 3,446
  • 10
  • 18
  • Some of the statements you make here aren't quite correct. "It will not access the standard input and output of the parent process" - not even sure what you mean by that, but the child process certainly still inherits those file handles, and can write to the same output channel as the parent process. "You should never use & for tasks that require user input" - in a script, that's probably more or less true, but in general, it's fine to background a process that requires user input; you will of course need to foreground it to provide the input. – davmac Jul 29 '16 at 13:06
  • You are partially right: it will inherit stdout/stderr, but not stdin. – Piotr Wilkin Jul 29 '16 at 13:11
  • Again, that's not correct. It will certainly inherit stdin. – davmac Jul 29 '16 at 13:12
  • Okay, you're right. It will inherit stdin, but it will block upon trying to read from it. – Piotr Wilkin Jul 29 '16 at 13:59
  • So, i've one more here, what are those background/foreground processes. I'm executing a java program and it has the code to do some task. After completion of that task why my prompt still waiting to run these background/foreground work? – Aditya Jul 30 '16 at 06:18