fg/bg: run command in background with user notification
Brandon S. Allbery
allbery at ncoast.ORG
Mon Jul 17 02:07:20 AEST 1989
Yet another attempt to put the sources back into alt.sources....
This is a little script that I've been using in various forms for a couple
of years now, on a number of System III/V systems. It's a variant of nohup;
it runs a command in the background and saves its output. There are three
differences from nohup, however:
* it notifies the user upon command completion by ringing the terminal
bell: twice if no error, 3 times if error
* on VT100-compatible terminals which have or emulate the keyboard LEDs,
it can inform the user of command completion using them: L1 is on while
the program is running, all LEDs off when the command terminates without
error, L2 on if the command terminates with an error
* if the basename of this script contains "fg", the foreground copy of the
script runs "tail -f" on the output file and the background copy will
kill the "tail" when it exits; this provides an "interruptable" pseudo-
foreground run
It's no substitute for a real window system, or even for job control, but
even windows aren't perfect for everything; I use this to do compiles a lot,
then inspect the logs for errors (bg) and/or load it into Emacs and let it
parse the error messages.
A quick example of the log file:
Sun Jul 16 12:05:21 EDT 1989
$$ mkshar bg
o - shar
a - bg
$$ exit 0
Sun Jul 16 12:05:33 EDT 1989
Shar(e) and enjoy.
++Brandon
#--------------------------------CUT HERE-------------------------------------
#! /bin/sh
#
# This is a shell archive. Save this into a file, edit it
# and delete all lines above this comment. Then give this
# file to sh by executing the command "sh file". The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# -rwxr-xr-x 1 allbery member 1294 Jul 16 12:05 bg
#
echo 'x - bg'
if test -f bg; then echo 'shar: not overwriting bg'; else
sed 's/^X//' << '________This_Is_The_END________' > bg
X:
X# fg/bg: run command in background, niced, with output to file
X# notify user upon command completion
X# if invoked as "fg", start "tail -f" on log file in foreground, killing
X# it automatically when background finishes
X#
X# remove ## from lines below to get notification on VT100s or close compatibles
X# via LEDs: L1 on when process running, L2 on if process exited with error,
X# all LEDs off if process exited without error (useful!)
X#
X# this version does not handle user logout (well); I have one which does a
X# slightly better job but is rather buggy, so I'm not releasing it
X#
X# I hereby release this script into the public domain. ++Brandon
X
Xcase $# in
X0) echo "usage: $0 command [args ...]" >&2
X exit 1
X ;;
Xesac
Xname=`basename $0 .sh`
X> ./$name.out
X{
X## [ "$TERM" = vt100 ] && echo '\033[q\033[1q\c'
X trap '' 3 2 1
X date >> ./$name.out
X echo "\$\$ $@" >> ./$name.out
X {
X eval nice -20 "$@"
X } >> ./$name.out 2>&1
X status=$?
X echo "\$\$ exit $status" >> ./$name.out
X date >> ./$name.out
X## [ "$TERM" = vt100 ] && echo '\033[q\c'
X if [ $status -ne 0 ]; then
X echo '\007\c'
X## [ "$TERM" = vt100 ] && echo '\033[2q\c'
X sleep 1
X fi
X echo '\007\c'
X sleep 1
X echo '\007\c'
X case "$name" in
X *fg*) kill -2 $$ 2> /dev/null ;;
X esac
X} &
Xcase "$name" in
X*fg*) exec tail -f ./$name.out ;;
Xesac
________This_Is_The_END________
if test `wc -c < bg` -ne 1294; then
echo 'shar: bg was damaged during transit (should have been 1294 bytes)'
fi
fi ; : end of overwriting check
exit 0
--
Brandon S. Allbery, moderator of comp.sources.misc allbery at ncoast.org
uunet!hal.cwru.edu!ncoast!allbery ncoast!allbery at hal.cwru.edu
Send comp.sources.misc submissions to comp-sources-misc@<backbone>
NCoast Public Access UN*X - (216) 781-6201, 300/1200/2400 baud, login: makeuser
* "ncoast" regenerates again! The 5th "ncoast", coming August 1 (stay tuned) *
More information about the Alt.sources
mailing list