[comp.unix.wizards] invoking write from cron $W could not be reached

news@investor.UUCP ( Bob Peirce) (04/27/89)

In article <2505@mentor.cc.purdue.edu> mjs@mentor.cc.purdue.edu (Mike Spitzer) writes:
>In article <8594@xanth.cs.odu.edu> kremer@cs.odu.edu (Lloyd Kremer) writes:
>>In article <175@dftsrv.gsfc.nasa.gov> tomc@dftsrv.gsfc.nasa.gov (Tom Corsetti)
>>writes:
>>
>>>I'm trying to set up a script that runs from cron, checks files
>>> ...
>>>        echo "message to send" | write userid
>>
>>It works on our System V Release 3.  Write also issues a message to stderr
>>reporting its inability to determine which terminal to use for replys, which
>>cron mails to the crontab owner, but the message itself gets through OK.
>
>Since Tom reported that it didn't work for him, I guess we can
>assume that he not using System V Release 3.  4.3BSD write(1) exits
>with a fatal error if it cannot find out what terminal the writer is
>logged on to.  So, write isn't going to work under 4.3BSD if run from
>inside cron.
>
>It's relatively simple to write a shell script version of write using
>echo, who, and awk that doesn't depend on a controlling tty.  You
>might want to try that (or consider using logger(1)/syslog(8)
>instead).

It doesn't work on our Sys V Rel 2 either.  If the write command isn't
generated from a terminal it dies.  We get around it by determining the
terminal the target user is logged into and catting directly to that
device.  If there is no write permission on the device we send mail.

#--------------------------  CUT HERE  ------------------------------
# -- xmt:  Attempt to get around write restriction in at/cron

PATH=:/usr/local/bin:/usr/local/sh:/bin:/usr/bin
export PATH

if [ $# -ne 2 ]
then
	echo 'usage:  xmt login "message"'
	exit 1
fi
W=$1
M=$2

#  Trap possible garbage if "to" is not logged in
set `who | grep $1 || echo XXX Unknown`
TO=$1
TTY=$2

U=$LOGNAME

if [ $TO = "XXX"  -o ! -w /dev/$TTY ]
then
	mail $U <<~
Subject: $W could not be reached
Message: $M
~
else
	cat >> /dev/$TTY <<~





		<insert ^G here>Mesasage from $U ...

$M





~
fi


-- 
Bob Peirce, Pittsburgh, PA				 412-471-5320
uucp: ...!{allegra, bellcore, cadre, idis, psuvax1}!pitt!investor!rbp
	    NOTE:  Mail must be < 30K  bytes/message

scott@grlab.UUCP (Scott Blachowicz) (05/02/89)

Another thing that can work depending on what you're trying to accomplish
is to use the wall program. I don't know what versions of UN*X this is
supported on, but on ours (HP-UX), you can specify a group name to
receive a broadcast message. I've got a script setup that checks for
available free space on different file systems. If it gets below a
threshold value for that file system a message is sent to the group
'sys'. I just add the regular logins for the people able to check for
and reclaim disc space to the 'sys' group. The script looks like this:

   #! /bin/sh
   if [ -z "$1" ]
   then tolerance=20000
   else
      tolerance=$1
      shift
   fi
   df_output=`df $* |
              sed 's/[()]/ /g' |
              awk '{if ($4 <= '$tolerance') {print}}'`
   if [ ! -z "$df_output" ]
   then
      ( uname -a ; echo "$df_output" ) | /etc/wall -g sys
      ( uname -a ; echo "$df_output" ) | remsh other_sys "/etc/wall -g sys"
   fi
---
Scott Blachowicz
USPS:  Graphicus                UUCP:    ...!hpubvwa!grlab!scott
       150 Lake Str S, #206     VoicePh: 206/828-4691
       Kirkland, WA 98033       FAX:     206/828-4236