diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index bef953409657..1931718320a9 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -1544,7 +1544,8 @@ protected Terminal buildTerminal(InputStream inputStream) throws IOException { if (inputStream != null) { // typically when there is a file script to read from terminal = TerminalBuilder.builder().streams(inputStream, getErrorStream()).build(); } else { // no input stream, normal operation: proper behavior needs a system terminal (which needs system streams) - terminal = TerminalBuilder.builder().system(true).dumb(false).streams(System.in, System.err).build(); + boolean hasTty = System.console() != null; + terminal = TerminalBuilder.builder().system(true).dumb(!hasTty).streams(System.in, System.err).build(); } this.terminalsToClose.add(terminal); return terminal; diff --git a/beeline/src/test/org/apache/hive/beeline/TestBeeLineBuildTerminal.java b/beeline/src/test/org/apache/hive/beeline/TestBeeLineBuildTerminal.java new file mode 100644 index 000000000000..2ad4bff12ef6 --- /dev/null +++ b/beeline/src/test/org/apache/hive/beeline/TestBeeLineBuildTerminal.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hive.beeline; + +import java.io.ByteArrayInputStream; + +import org.jline.terminal.Terminal; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Test; + +public class TestBeeLineBuildTerminal { + + @Test + public void testBuildTerminalWithoutTtyDoesNotThrow() throws Exception { + Assume.assumeTrue("Requires no controlling TTY", System.console() == null); + BeeLine beeLine = new BeeLine(); + try { + Terminal terminal = beeLine.buildTerminal(null); + Assert.assertNotNull("buildTerminal must return a Terminal without a TTY", terminal); + } finally { + beeLine.close(); + } + } + + @Test + public void testBuildTerminalWithInputStream() throws Exception { + BeeLine beeLine = new BeeLine(); + try { + Terminal terminal = beeLine.buildTerminal(new ByteArrayInputStream(new byte[0])); + Assert.assertNotNull("buildTerminal must return a Terminal for a script input stream", terminal); + } finally { + beeLine.close(); + } + } +} diff --git a/bin/hive b/bin/hive index 41ba504d8a52..8f92b9e6a739 100755 --- a/bin/hive +++ b/bin/hive @@ -337,9 +337,8 @@ else fi if [[ "$SERVICE" =~ ^(hiveserver2|beeline|cli)$ ]] ; then - # If process is backgrounded, don't change terminal settings - if [[ ( ! $(ps -o stat= -p $$) =~ "+" ) && ! ( -p /dev/stdin ) && ( ! $(ps -o tty= -p $$) =~ "?" ) ]]; then - export HADOOP_CLIENT_OPTS="$HADOOP_CLIENT_OPTS -Djline.terminal=jline.UnsupportedTerminal" + if [[ ! -t 0 ]]; then + export HADOOP_CLIENT_OPTS="$HADOOP_CLIENT_OPTS -Dorg.jline.terminal.dumb=true" fi fi