Sam Trenholme's webpage
This article was posted to the Usenet group alt.hackers in 1995; any technical information is probably outdated.

Timed rup

Article: 7642 of alt.hackers
From: (Ashley)
Newsgroups: alt.hackers
Subject: Timed rup
Date: 12 Apr 1995 17:36:59 +0100
Organization: University of Warwick, Coventry, UK
Lines: 123
Approved: bolin@cheese.corporation
Message-ID: 3mgvjb$
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Status: RO


The UNIX command 'rup' on our local system just hangs if a machine is
down. I want this command to return an error message if this happens and
not just hang because I am hooking the command to a telnet port.

The following nasty piece of code runs the rup command, and a sleep command
in the background. They both write to a file - the rup when it has a
result, and the sleep code writes an error message after the TIMEOUT
period. The process in the foreground waits until one of these has written
to the file, and then gives the result accordingly.

Does anybody know a better way? Child processes cannot pass variables back
up to the parent so I had to use the temporary file. I have some code that
implements a version of rusers with timeout. It uses the portmapper to make
the RPC call, using pmap_rmtcall, but I cannot see how it works (C? Yuk!!
Shell rules OK!). Is it possible to tell if a process has terminated or not
without using ps?

# localload
# Ashley Ward 24/11/93 (
# prints out load on the local machines, and does not hang if a machine
# is down
# Improvements
# ~~~~~~~~~~~~
# - Speed it up! Maybe optimise by removing comments, shortening variable
#   names etc or rewrite in a compiled language (groan)

# Commands used

# Files used
# Decide on the temporary file's name - depends on the PID of this shell, so
# should be fairly unique hopefully

# Variables
# This is the maximum time (secs) that the rup command is allowed to take

# -----------------------
# This function implements a version of 'rup' that does not hang if the
# machine being queried is down (Timed RUP)
# MACHINE must be set to a machine name - the result or an error message
# is written to stdout

# Do the COMMAND
# Insert this command for testing!
) > $TEMP &

# CHECK there is a result after a length of time
if [ ! -s "$TEMP" ]
	# The file still is length 0 or does not exist
	# So write an error message to it
	$ECHO $MACHINE" not responding" > $TEMP
) &

# Loop until either we have a result from COMMAND, or CHECK has decided
# we have run out of time (something will appear in the temporary file)
while ([ ! -s "$TEMP" ])
	# Just waste time...

# We either have the result of COMMAND or an error message from CHECK now,
# so write it out

# Kill both processes - this is a bit risky because they may have terminated
# already and another process could be running under the same ID, but this
# is unlikely??
$KILL $CHECKPID 1>/dev/null 2>/dev/null
$KILL $COMMANDPID 1>/dev/null 2>/dev/null

# Remove the temporary file - do this _after_ both processes have terminated
# otherwise it could be recreated
$RM -f $TEMP
# -----------------------

MACHINES="holly lily clover crocus violet poppy"

Change the MACHINES variable to your local names, and the locations of the
commands at the top (I have to do this because the $PATH may be a mess when
I run it).


   __	___  _	_  __	 ___  _  _
| (  ) / __)( )( )(  )	(  _)( \/ )    Mumble mumble |
| /__\ \__ \ )__(  )(__  ) _) \  /   ... thank you!|
|(_)(_)(___/(_)(_)(____)(___)(__/     |

Back to index