1

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

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
  • Not getting anything back? – AirCombat Oct 2 '15 at 14:23
  • Output: pastebin.com/g9Up8YMN – AirCombat Oct 2 '15 at 14:24
  • If you run who, is root logged in multiple times? – Mark Plotnick Oct 2 '15 at 14:25
  • Yes, tty1 and pts/0 – AirCombat Oct 2 '15 at 14:26
  • 1
    root often has mesg n set. You would need to mesg y on the terminals to receive the messages. – roaima Oct 2 '15 at 14:30
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
  • 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 ??? – AirCombat Oct 2 '15 at 14:01
  • Like grep pts/0 from who an redirect to that? – AirCombat Oct 2 '15 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.