Skip to content

setup/questions.sh: Auto-detect headless mode when no terminal is available #2557

@robertpopa22

Description

@robertpopa22

Problem

Running sudo mailinabox via SSH without a PTY (e.g., from automation scripts, paramiko, Ansible, or cron) fails with:

Error opening terminal: unknown

This happens because dialog (ncurses) is called in setup/questions.sh when NONINTERACTIVE is not set, and dialog requires a terminal.

Workaround: Setting NONINTERACTIVE=1 sudo mailinabox works, but this is not documented and not intuitive — especially for reconfiguration scenarios where all settings are already in the env file.

Expected Behavior

When running sudo mailinabox without a terminal attached (no PTY), the script should automatically detect this and either:

  1. Set NONINTERACTIVE=1 automatically, or
  2. Print a helpful error message suggesting NONINTERACTIVE=1

Proposed Fix

In setup/questions.sh, add terminal detection at the top:

# Auto-detect headless mode when no terminal is available
if [ -z "${NONINTERACTIVE:-}" ] && ! [ -t 0 ]; then
    # No terminal attached (SSH without PTY, cron, pipe, automation)
    # dialog can't work without a terminal, so auto-set NONINTERACTIVE
    export NONINTERACTIVE=1
    echo "Notice: No terminal detected, running in non-interactive mode."
    echo "To force interactive mode, run with: script -qc 'sudo mailinabox' /dev/null"
fi

[ -t 0 ] checks if stdin is a terminal. If not (SSH without PTY, cron job, pipe), it automatically enables non-interactive mode and prints a notice.

Use Case

This is especially relevant for:

  • Automation: Ansible, Puppet, scripts managing MiaB remotely
  • IP migration: When changing PUBLIC_IP in mailinabox.env and re-running sudo mailinabox via SSH automation
  • CI/CD: Automated testing or deployment pipelines

Environment

  • Mail-in-a-Box v74
  • Ubuntu 22.04.5 LTS
  • Running via Python paramiko SSH (no PTY allocated)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions