Fixing Jenkins Error: The input device is not a TTY

Updated: February 3, 2024 By: Guest Contributor Post a comment

The Problem

When automating tasks with Jenkins, particularly those involving Docker, you might encounter the dreaded The input device is not a TTY error. This error typically arises when an interactive terminal is expected by a command running under Jenkins, but no such terminal is available. This guide explores the causes and provides tested solutions to address this problem.

Why does it happen?

This error generally occurs because Jenkins jobs run in a non-interactive shell environment by default, which doesn’t provide a TTY (TeleTYpewriter) for command execution. Certain operations or tools, such as Docker, often rely on a TTY when running interactively. Therefore, the lack of a TTY causes these operations to fail when automated in Jenkins pipelines.

Solution #1: Use -T Flag with Docker Commands

Passing the -T flag with Docker commands disables pseudo-TTY allocation. This is useful for running commands that do not require user interaction inside Jenkins.

Steps to Implement

  1. Add -T to your Docker command in the Jenkinsfile.
  2. Ensure that commands dependent on a TTY are modified or removed.

Example:

docker run -T my-image command

Output: The expected outcome of the command run without TTY error.

Notes

This solution is straightforward and solves the problem for Docker commands specifically. It may not be suitable for commands that genuinely require a TTY.

Solution #2: Use Scripted Pipeline Instead of Declarative

Switching from a declarative to a scripted Jenkins pipeline can give you more control over the execution environment, potentially mitigating the TTY error.

Steps to Implement

  1. Rewrite your Jenkinsfile as a scripted pipeline.
  2. Use the sh command for shell execution without requiring a TTY.

Example:

node {
    stage('Run without TTY') {
        sh 'run_some_command_here'
    }
}

Expected result: Execution of the pipeline stage without encountering the TTY error.

Notes

This approach provides greater flexibility but requires a deeper understanding of Jenkins pipeline syntax. It may not be suitable for all Jenkins users.

Solution #3: Redirect STDIN, STDOUT, STDERR

By redirecting standard input, output, and error, commands that might otherwise expect a TTY can be tricked into running without it.

Steps to Implement

  1. Use redirection operators in your shell commands within the Jenkins pipeline.
  2. Verify the command’s compatibility with redirection.

Example:

sh 'command </dev/null >output.log 2>&1'

Output: Successful execution, with command output available in output.log.

Notes

The effectiveness of this method can vary based on the command’s requirements and its interaction with input/output streams. Some commands may not operate correctly without an actual TTY.

Conclusion

Encountering the The input device is not a TTY error in Jenkins does not have to be a deadlock. By understanding the cause and applying appropriate solutions as described above, most instances of this error can be effectively managed. Each solution has its context where it shines, along with limitations. Evaluating the specifics of your situation will help in selecting the most effective strategy.