Sling Academy
Home/DevOps/Fixing Jenkins Error: The input device is not a TTY

Fixing Jenkins Error: The input device is not a TTY

Last updated: February 03, 2024

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.

Next Article: Jenkins: How to disable SonarQube analysis for a specific branch

Previous Article: Jenkins Error: groovy.lang.GroovyObject is not permitted to be deserialized

Series: Jenkins Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide