Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!rochester!pt.cs.cmu.edu!cadre!pitt!darth!investor!news From: news@investor.UUCP ( Bob Peirce) Newsgroups: comp.unix.wizards Subject: Re: invoking write from cron $W could not be reached Message-ID: <1078@investor.UUCP> Date: 27 Apr 89 13:37:11 GMT References: <175@dftsrv.gsfc.nasa.gov> <8594@xanth.cs.odu.edu> <2505@mentor.cc.purdue.edu> Reply-To: rbp@investor.UUCP (Bob Peirce #305) Distribution: usa Organization: Cookson, Peirce & Co., Pittsburgh, PA Lines: 79 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 <<~ 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