Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The purpose is to restart the machine if it doesn't get an abort command from the user. The problem is that the way the terminals are set up, user must supply SU password for shutdown, init, reboot, etc. commands. We are wondering if it's possible to supply password in bash script itself, maybe using stdin. i.e.:

# (script)
echo "Terminal will restart in 60s, Ctrl+C to cancel."
sleep 60
su - root -c "init 6" < "password"

We are going to try putting the password in a file and stdin that.

Edit: sudo and expect are out as solutions as getting them installed will be impossible due to a massive amount of paperwork for permission and a limited timeframe. Also we would like to use init 6 or shutdown -r rather than reboot.

share|improve this question

You can configure sudo to allow any user to run certain commands you specify, as root, without having to type a password. See this Server Fault article for a description of how to configure sudo.

Then, in your script, use sudo rather than su:

sudo reboot

share|improve this answer
That isn't an option with our current setup, sudo is not installed, and I'm just learning Expect is also out of the question, as getting them installed on all the legacy terminals on our network is near impossible. – Kendall Aug 8 '12 at 12:36

AFAIK you cannot do what you are asking for. When you are using stdin with <, the password will be input via stdin much before the shell actually asks for the password. You should try and use sudo. If not, your best bet is to chmod reboot and give yourself execute privileges.

share|improve this answer

You can do it using socat:

user@host$ (sleep 1; echo SuperSecr3t) | socat - EXEC:'su -c id',pty
Password: ### No input here, just wait 1sec
uid=0(root) gid=0(root) groups=0(root)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.