1

I'm completely puzzled by this behavior of Tomcat 9.0.21 startup script catalina.sh:

# following line added by me
echo "CATALINA_HOME"
# following line added by me
echo $have_tty

if [ $have_tty -eq 1 ]; then
  # following line added by me
  echo "CATALINA_HOME"

  echo "Using CATALINA_BASE:   $CATALINA_BASE"
  echo "Using CATALINA_HOME:   $CATALINA_HOME"
  echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"
  if [ "$1" = "debug" ] ; then
    echo "Using JAVA_HOME:       $JAVA_HOME"
  else
    echo "Using JRE_HOME:        $JRE_HOME"
  fi
  echo "Using CLASSPATH:       $CLASSPATH"
  if [ ! -z "$CATALINA_PID" ]; then
    echo "Using CATALINA_PID:    $CATALINA_PID"
  fi
fi

Now when I run catalina version | grep CATALINA_HOME I cannot see any result with the original script version. In my modified version of script I can only see what's echo-ed before the 'if have_tty' section!

catalina | grep CATALINA
CATALINA_HOME

I'd expect to see also the next CATALINA_HOME echo-ed from within the if block as well as the "Using CATALINA_HOME ..." line.

What's going on there?

(If it matters I'm running Mac OS X 10.14.5 with iTerm and zsh as my shell)

UPDATE

People commented that it's because the have_tty variable isn't set but that's not a problem in my case. When I just run catalina version I get the full output:

CATALINA_HOME
1
CATALINA_HOME
Using CATALINA_BASE:   /usr/local/Cellar/tomcat/9.0.21/libexec
Using CATALINA_HOME:   /usr/local/Cellar/tomcat/9.0.21/libexec
Using CATALINA_TMPDIR: /usr/local/Cellar/tomcat/9.0.21/libexec/temp
Using JRE_HOME:        /Users/jumar/.jenv/versions/1.8.0.171
Using CLASSPATH:       /usr/local/Cellar/tomcat/9.0.21/libexec/bin/bootstrap.jar:/usr/local/Cellar/tomcat/9.0.21/libexec/bin/tomcat-juli.jar
Server version: Apache Tomcat/9.0.21
...

It's just with the pipe (grep) that things start looking weird:

catalina version | grep CATALINA_HOME
CATALINA_HOME
# here should be the missing "Using CATALINA_HOME..."
  • 2
    it means that $have_tty is not equal to 1. You'll have to look how it's previously set – A.B Jun 13 at 13:12
  • 2
    It seems that according to this test your standard output is not considered to be using a terminal. You have to check out why in your particular context. – Zeitounator Jun 13 at 14:01
  • 1
    using @Zeitounator's comment, reproducer: test -t 1; echo $? versus (test -t 1; echo $? ) | cat . Usually the test is done on stdin (eg the tty command), but here it's done on stdout – A.B Jun 13 at 14:15
  • Thanks for the comments but have_tty is not a problem. it is set to 1. When I remove the pipe (grep) then I can see everything in the console. It's just with the pipe that the things start going wild. Not sure exactly what it does but test -t 1; echo $? returns 0. – Juraj Martinka Jun 14 at 5:24
  • So it seems that using the catalina script in the pipe makes the if [ -t 1 ]; fail and have_tty is set to zero in that case. I was puzzled because using plain catalina sets have_tty properly to 1. Still not sure if that's a standard behavior or not. – Juraj Martinka Jun 14 at 12:21
2

First thanks to @Zeitounator and @A.B for their comments!

After some testing on different tomcat versions and digging deeper I've found, that indeed the issue is in have_tty being set to zero. Using the catalina script in the pipe makes the if [ -t 1 ]; test fail.

I was puzzled because using plain catalina sets have_tty properly to 1. It was even weirder because it used to work with previous minor tomcat version. It was in fact introduced in this commit: https://github.com/apache/tomcat/commit/bf894a0e6b5c8f92b122452645e93d63277ad3ab

It seems that the -t 1 test isn't appropriate when used in a pipe. Instead, it should be -t 0 (this is per A.B's comment "Usually the test is done on stdin").

0

Remove the | grep CATALINA from your test command and if you're not getting any output from the line:

echo $have_tty

Then $have_tty is not set and would then cause execution to not enter the if block. You would have to trace where $have_tty is getting set originally.

It looks like catalina uses this command to determine if it has a tty:

$ tty
/dev/ttys011

To get a tty, be sure the script is run from the command line and not through a non-tty process (e.g. init process)

  • Thanks for the answer but this is not a problem. have_tty variable is set. When I remove the pipe (grep) then I can see everything in the console. It's just wit the pipe that the things start going wild... – Juraj Martinka Jun 14 at 5:21
  • What does this produce? catalina | grep '.*' – jjj Jun 14 at 6:20
  • it's similar to the example in my question. Basically it produces the output as just catalina BUT the "Using ..." messages aren't present. – Juraj Martinka Jun 14 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.