Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Trying to write a bash script which will open a screen, start a python application, and interact with the python interaction as required when it is first started.

If not possible in screen, I would be happy to switch to tmux as an alternative. I am just not that great at bash scripting and have no experience with expect.

Here is what I have so far:

sent=blah
expexec=$(/usr/bin/expect -c "/usr/bin/python /opt/app.py expect \"Interaction:\" send \"$sent\r\" interact")
/usr/bin/screen -L -dmS spawner /bin/sleep 30
/bin/sleep 1
/usr/bin/screen -S spawner -X screen screen -L -dR myDaemon
/bin/sleep 1
/usr/bin/screen -S myDaemon -X detach
/usr/bin/screen -S myDaemon -X stuff "echo $expexec"
/usr/bin/screen -S spawner -X stuff "exit\r"

Hoping someone can help me...

Above is the latest iteration, where I have tried to write the entire command as seen in another example in a variable. The screen commands all work, but I can't get expect to correctly execute and look for the input queue and send the response.

Thanks in advance,

JG

share|improve this question
add comment

1 Answer

Just in case this will help anyone else, I thought I would let everyone know how I got this working. I simply split my one script into two, with the expect portion in its own script. My version only makes use of one variable, but it is easily scalable I guess.

expect script:

#!/usr/bin/expect
set timeout 60

set sent [lindex $argv 0]

spawn /usr/bin/python /opt/app.py

expect "Interaction:"

send "$sent\r";

interact

main script:

#!/bin/sh

# variables
sent=blah
expectScript=/path/to/expect/script

# bash screen detachment foo
/usr/bin/screen -L -dmS spawner /bin/sleep 30
/bin/sleep 1
/usr/bin/screen -S spawner -X screen screen -L -dR myDaemon
/bin/sleep 1
/usr/bin/screen -S myDaemon -X detach

# sending the command to the screen
/usr/bin/screen -S myDaemon -X stuff "$expectScript $sent\r"

# close the screen spawner
/usr/bin/screen -S spawner -X stuff "exit\r"

JG

share|improve this answer
add comment

Your Answer

 
discard

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.