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