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: esvfh@csv.warwick.ac.uk (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$fkg@holly.csv.warwick.ac.uk
NNTP-Posting-Host: holly-fddi.csv.warwick.ac.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Status: RO

ObHack:

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?

-snip--snip--snip--snip--snip--snip--snip--snip--snip--snip--snip--snip-
#!/bin/sh
#
# localload
#
# Ashley Ward 24/11/93 (ashley@dcs.warwick.ac.uk)
# 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
ECHO=/usr/bin/echo
RUP=/usr/ucb/rup
SLEEP=/usr/bin/sleep
CAT=/usr/bin/cat
RM=/usr/bin/rm
KILL=/usr/bin/kill

# Files used
# Decide on the temporary file's name - depends on the PID of this shell, so
# should be fairly unique hopefully
TEMP=/dcs/93/ashley/Daemon/localload/temp.$$

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

# -----------------------
# 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
#
TRUP ()
{

# Do the COMMAND
(
# Insert this command for testing!
#$SLEEP 5
$RUP $MACHINE
) > $TEMP &
COMMANDPID=$!

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

# 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" ])
do
	# Just waste time...
	:
done

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

# 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"
for MACHINE in $MACHINES
do
	TRUP
done
-snip--snip--snip--snip--snip--snip--snip--snip--snip--snip--snip--snip-

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).

Cheers!

Ash.
--
   __	___  _	_  __	 ___  _  _
| (  ) / __)( )( )(  )	(  _)( \/ ) esvfh@csv.warwick.ac.uk    Mumble mumble |
| /__\ \__ \ )__(  )(__  ) _) \  /  ashley@dcs.warwick.ac.uk   ... thank you!|
|(_)(_)(___/(_)(_)(____)(___)(__/   http://www.csv.warwick.ac.uk/~esvfh/     |



Back to index