Cron Bash Script - echo to current terminal instead of /var/spool/mail/root

6,334

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
Share:
6,334

Related videos on Youtube

ZZ9
Author by

ZZ9

Updated on September 18, 2022

Comments

  • ZZ9
    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
    ZZ9 over 8 years
    I 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
    ZZ9 over 8 years
    Like grep pts/0 from who an redirect to that?
  • ZZ9
    ZZ9 over 8 years
    Not getting anything back?
  • ZZ9
    ZZ9 over 8 years
  • Mark Plotnick
    Mark Plotnick over 8 years
    If you run who, is root logged in multiple times?
  • ZZ9
    ZZ9 over 8 years
    Yes, tty1 and pts/0
  • Mark Plotnick
    Mark Plotnick over 8 years
    Does echo hi | write root pts/0 work?
  • roaima
    roaima over 8 years
    root often has mesg n set. You would need to mesg y on the terminals to receive the messages.
  • ZZ9
    ZZ9 over 8 years
    That works. Thanks! Any idea how to hide the end of file flag?
  • roaima
    roaima over 8 years
    @AirCombat the "EOF" text is part of write
  • Mark Plotnick
    Mark Plotnick over 8 years
    Did 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?