0

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

1 Answer 1

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .