How to get a tty to respawn x number of times then pauses y number of seconds then repeat.

Asked by nbschock on 2009-06-26

I am using Fedora 11 and Upstart 0.3.9.24.fc11. I am using qingy to autologin into a c3270 mainframe emulation session. If the session is unavailable, the tty closes and respawns. What I am trying to do is have the tty respawn 10 times then pauses for 5 minutes if it is unsuccessful then try 10 more times repeating this process until it is successful. This worked on Fedora 5 when we placed the following line in the /etc/inittab file:

3:2345:respawn:/usr/local/sbin/qingy tty3

My current /etc/event.d/tty3 file looks like this:

_______________________________________________________________

start on stopped rc2
start on stopped rc3
start on stopped rc4

stop on runlevel 0
stop on runlevel 1
stop on runlevel 6

env COUNTER=1 ##Assuming this is resetting my counter, but I cannot find another way to set the initial
                ##value of the COUNTER.

pre-start script

if [ $COUNTER -eq 10 ] ; then
   COUNTER=1
   export COUNTER
   sleep 300
else
   sleep 5
   COUNTER=$(( $COUNTER + 1 ))
   export COUNTER
fi
end script

respawn
exec /usr/local/sbin/qingy tty3

____________________________________________________________

If the session fails and tty3 is stopped it will respawn and go through the Else part of the statement above causing it to sleep for 5 seconds then respawn again. However the incremented COUNTER variable is never saved and the IF part of the statement is never true.

My questions are:

1. Is there another way to set the initial COUNTER value to 1 that doesn't wipe out the incremented values?

2. Has anyone found a better approach to getting a tty to respawn like this?

Question information

Language:
English Edit question
Status:
Solved
For:
upstart Edit question
Assignee:
No assignee Edit question
Solved by:
Scott James Remnant (Canonical)
Solved:
2009-06-29
Last query:
2009-06-29
Last reply:
2009-06-29

On Fri, 2009-06-26 at 17:30 +0000, nbschock wrote:

> 1. Is there another way to set the initial COUNTER value to 1 that
> doesn't wipe out the incremented values?
>
You could use a file in /var/run that you modify and source.

> 2. Has anyone found a better approach to getting a tty to respawn like this?
>
There's an open wishlist bug to include this support directly into
Upstart.

Scott
--
Scott James Remnant
<email address hidden>

nbschock (nbschock) said : #2

Thanks Scott James Remnant, that solved my question.

nbschock (nbschock) said : #3

My /etc/event.d/tty3 pre-start script ended up looking like this:

pre-start script

sleep 3
touch /var/run/counter
echo 'COUNTER=$(( COUNTER + 1 ))' >> /var/run/counter
source /var/run/counter
echo "Trying to reconnect..." > /dev/tty3

if [ $COUNTER -ge 10 ] ; then
   echo "Session unable to connect, will try again later." > /dev/tty3
   rm -f /var/run/counter
   sleep 300
fi
end script