Cron Bash Script - echo to current terminal instead of /var/spool/mail/root
Solution 1
You can use the write utility to send text to a specific logged-in user.
command that produces output | write root
The documentation further explains:
To write to a user who is logged in more than once, the terminal argument can be used to indicate which terminal to write to; otherwise, the recipient's terminal is selected in an implementation-defined manner and an informational message is written to the sender's standard output, indicating which terminal was chosen.
On Red Hat/CentOS, the implementation-defined manner is to pick the terminal with the shortest idle time.
If you want to be able to write to one of several users who may be logged in, you can do something like this:
for u in root alice bob charlie
do
if users|grep -w -q $u
then
user=$u
break
fi
done
if test -n "$user"
then
command that produces output | write $user
fi
Solution 2
There is no "current terminal" when running from cron
.
By default, cron
sends an email containing output from the job. Your local mail subsystem delivers that to the file /var/spool/mail/$USER
, and you can read it using mail
, mailx
, or your preferred local email client.
There's no particular reason why you couldn't get your cron
job to write its output to a file in your home directory, for example like this:
* * * * * date >$HOME/.current_date 2>&1
If you're running a GUI you can use notify-send
to write messages in a pop-up on your screen. BUT it's not straightforward to do this from cron
. Have a search around StackExchange for solutions to this sub-problem.
If you want to write to a tty you can indeed use something like echo hello, world >/dev/tty1
. You would probably want to ensure that the right account was indeed logged on to /dev/tty1
before writing to it (this can be done by checking the ownership of the device, stat -c %U /dev/tty1
).
#!/bin/bash
#
me=roaima # userid to write messages to
log()
{
local tty owner
for tty in $( who | awk -v me="$me" '$1 == me {print $2}' )
do
owner="$( stat -c %U "/dev/$tty" 2>/dev/null )"
[[ "$me" = "$owner" ]] && echo "$@" >"/dev/$tty"
done
}
# ...
log "hello, world"
log "this is a message for you to read RIGHT NOW"
exit 0
However, you might be better off using the write
command. For example, to write to a user "roaima" on a logged in terminal you could do this:
* * * * * echo hello from cron | write roaima >/dev/null 2>&1
Related videos on Youtube
ZZ9
Updated on September 18, 2022Comments
-
ZZ9 almost 2 years
Is it possible for a Cron Bash Script to echo to the current session terminal instead of /var/spool/mail/root
I have a script which writes errors to a log file but any supplemental/unimportant info I echo out to the terminal.
When I run the script in cron as root it redirects the messages to /var/spool/mail/root rather than the terminal.
I would like them to be shown in the terminal if root or another user is logged in rather than stored. If these messages are lost when nobody is logged in thats fine. Like on Cisco IOS
-
ZZ9 over 8 yearsI have had a search around. There is no GUI on my servers. So its not possible to redirect to something like /dev/tty7 etc ???
-
ZZ9 over 8 yearsLike grep pts/0 from who an redirect to that?
-
ZZ9 over 8 yearsNot getting anything back?
-
ZZ9 over 8 yearsOutput: pastebin.com/g9Up8YMN
-
Mark Plotnick over 8 yearsIf you run
who
, is root logged in multiple times? -
ZZ9 over 8 yearsYes, tty1 and pts/0
-
Mark Plotnick over 8 yearsDoes
echo hi | write root pts/0
work? -
roaima over 8 years
root
often hasmesg n
set. You would need tomesg y
on the terminals to receive the messages. -
ZZ9 over 8 yearsThat works. Thanks! Any idea how to hide the end of file flag?
-
roaima over 8 years@AirCombat the "EOF" text is part of
write
-
Mark Plotnick over 8 yearsDid you need to do
mesg y
for it to work? I didn't need to do so on CentOS 7 - my root login allowed messages by default, and root could write to my regular user which had messages turned off. But it may be different in other releases. What release are you running?