Server Fault is a question and answer site for system and network administrators. 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

I have a bunch of several bash scripts that will perform some actions that require user input, therefore I'm using expect with it.

However, I need one to keep running. But if I use expect, the expect command will wait while the script is running and my main script will not continue.

IE: script1 spawns a script as root:

expect << EOF
spawn su root -c "/bin/bash"
expect "Password:"
send "test\n"
set timeout -1
expect eof;
EOF will spawn a screen:

screen -dm will spawn a binary that must keep running:

expect << EOF
spawn mybinary
expect "*Enter password for my binary"
send "test\r"
expect "*I am running"
set timeout -1
expect -re . { exp_continue } eof { exit }
expect eof;

But, since one script is waiting for expect to finish, my main script won't continue.

Any way to do that?

share|improve this question
Can you change E.g., sudo – Mark Wagner Jun 15 '11 at 16:14

First, you absolutely don't want to send a password with expect. Besides being very bad style it totally undermines the security of your system because you have to store a user password in cleartext. And everybody in this day and age knows by heart that you should never store any passwords in cleartext, ever.

I would recommend reading up on the manpage for sudoers. Then you can use sudo from within the script without having to store passwords in the clear. A more solid solution would probably be an executable with suid root. Shell scripts can't have suid for security reasons, so this would require compiling a small program.

On the other hand, if you are sure that you need multiple processes running in parallel communicating with each other, then bash version 4 offers a solution in form of the coproc keyword. Basically you put coproc in front of a command line. For simple commands it has roughly the same effect as an & at the end of the command line; the process will run in parallel with your script. The difference is that with coproc you have the stdout and stdin file descriptors of the other process available in the array COPROC, so your script can communicate with the other process. See

Then you also might want to wait on the other process before your script exits.

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.