1

Please give me advices about expect scripting combined with an upstart script.

There are 3 scripts that I am dealing with right now.

Script A) Java program

   PATH: /opt/~~/manager

Script B) Upstart script to respawn Script A

   PATH: /etc/init/xx.conf

Script C) Health check program – Pushes Script A to send a command to a process in another server and confirms the status of the connection. Run by cron.

   PATH: /opt/x~x/healthcheck.sh

What I want to do is to

1) Restart the Java process every time the server reboots or process dies - This has been achieved with Script B.

2) Keep the connection on hold between the Java process and the process in another server by running a health check program (Script C) regularly and what it does is send a link command to the neighbor server process via telnet.

I have been failing with the above mentioned “2” and I would like to ask for advices.

Currently my script is as follows:

Script B)

description “description”

start on runlevel [2345]

stop on runlevel [!2345]

env USER=executinguser

respawn
respawn limit 2 10

script

      while [ ! "`ifconfig -s | grep eth1`" ]
      do
        sleep 5
      done

      su - ${USER} -c "source /opt/~~/.bash_profile && \
      /opt/~~/manager start | \
      tee -a  /opt/~~.log"
    end script

pre-start script

      RET=`su - ${USER} -c "source /opt/~~/.bash_profile && \
      /opt/~~/manager status > /dev/null" || echo $?`
      if [ "$RET" -eq "0" ] || [ -z "$RET" ] ; then
        su - ${USER} -c "source /opt/~~/.bash_profile && \
        /opt/~~/manager force-stop"
      fi
    end script

pre-stop script

      su - ${USER} -c "source /opt/~~/.bash_profile && \
      /opt/~~/manager r force-stop"
    end script

Above script works fine and the process respawns without an error.

Then to add an automatic process routing to this script, I have added this line after the main “script” section to call out an expect script.

/usr/bin/sh -f /etc/init/autolink.sh

And changed permission of the file to give an exec permission. -rwxr-xr-x 1 root root 207 May 14 11:25 autolink.sh

/etc/init/autolink.sh

#!/usr/bin/expect -f

puts "First test"

set CMD "link routename IPaddress Port"
set HOST "localhost"

  spawn telnet ${HOST}
  expect “`^]`"
  send \"${CMD}\n\"
  expect "000"

and I have also tried the following


#!/usr/bin/expect -f

puts "First test"

  spawn telnet IPaddress Port
  expect “`^]`"
  send "link routename IPaddress Port"
  expect "000"

I have tried calling out the expect script with different path on Script B, and tried naming the expect script with different extension and shebang but none of them worked, in fact none of the options I have tried wouldn’t even read “puts “First test” line or reach any part of the expect script it seemed.

When I run the expect script on its own – # ./autolink.sh

It runs just fine, it receives “^]” then it doesn’t receive “000” in the end then exits after sending the link command.

I have also tired <1> inserting the expect script into a variable in the upstart script and echo-ing the variable or <2> getting into the expect part of the script with “expect –c “ ~~” or “/usr/bin/expect <

My question is – how should I call a piece of expect script in a shell script? And how should an expect script be written with an appropriate shebang and commands?

Thanks very much for your help in advance.

  • I see some curly quotes in your code. Make sure you using plain double quotes. – glenn jackman May 17 '18 at 14:46
  • In expect script {} is not to be used? The latter script is more appropriate as an expect script? – AiAmAi May 18 '18 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.