Software⏱️ 4 min read📅 2026-06-11

How to Fix: Bash & 'su' script giving an error "standard in must be a tty"

Bash script error fix for standard in must be a tty

Quick Answer: Use "su - ${RUNAS} -c" instead of "su ${RUNAS} -c" to ensure the command is run as a login shell.

The error 'standard in must be a tty' occurs when the standard input is not connected to a terminal, which can cause issues with certain commands or scripts. This issue affects users who run bash scripts that rely on command-line input, such as the Hudson application script you provided.

This error can be frustrating because it prevents the script from functioning correctly, and it may require additional troubleshooting steps to resolve. In this guide, we will walk through the possible causes of this error and provide two primary fix methods to help you resolve the issue.

🔍 Why This Happens

  • The first main reason why this error happens is because the 'su' command is not being used correctly. When using 'su', it's essential to specify a tty (terminal) as an argument, like so: su -c 'command' -t tty. However, in your script, you are using '-c' instead of '-t'. This tells 'su' to execute the specified command directly, without using the standard input.
  • Another possible reason for this error is that the script is trying to run a command that requires standard input, but the standard input is not connected to a tty. For example, if you are running a command that requires user input, such as 'echo' or 'read', and you are not providing any input, the script will fail with this error.

🚀 How to Resolve This Issue

Fixing the 'su' Command

  1. Step 1: To fix this issue, modify your script to use the correct syntax for the 'su' command. Replace '-c' with '-t', like so: su - ${RUNAS} -t tty -c "nohup java -jar ${HOME}/${BINARY} >> ${HOME}/${LOG} 2>&1; echo $! > ${HOME}/${PID}" &
  2. Step 2: Additionally, ensure that the standard input for commands is not being redirected to a file or other device. If you need to run a command with standard input, make sure it's connected to a tty.
  3. Step 3: You can also use the '-s' option with 'su' to specify the shell to be used, like so: su -s /bin/sh ${RUNAS} -t tty -c "nohup java -jar ${HOME}/${BINARY} >> ${HOME}/${LOG} 2>&1; echo $! > ${HOME}/${PID}" &

Alternative Fix Method

  1. Step 1: If the above fix doesn't work, you can try running the script as a non-interactive user. Replace 'su' with '/bin/bash' and remove the '-c' option, like so: /bin/bash - ${RUNAS} && nohup java -jar ${HOME}/${BINARY} >> ${HOME}/${LOG} 2>&1; echo $! > ${HOME}/${PID}
  2. Step 2: However, this method may require additional modifications to your script and may not work if the Hudson application relies on user input or other interactive features.

✨ Wrapping Up

By following these steps, you should be able to resolve the 'standard in must be a tty' error in your bash script. Remember to test your script thoroughly after making changes to ensure that it's working correctly and functioning as expected.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions