16

I have a program that prints some output that I would like to pop up on my terminal once every hour during the workday.

$ crontab -l
0 07-17 * * * /home/dat/scripts/cron.out

syslog reports the following:

(dat) CMD (/home/dat/scripts/cron.exe)
Jun 18 12:02:01 picard CRON[10848]: (CRON) info (No MTA installed, discarding output)

What am I missing?

1
  • cron jobs are not supposed to write to a terminal - there might be none available.
    – guntbert
    Jun 18, 2013 at 19:55

3 Answers 3

17

The dirty way could be to redirect the output of your program to the pts file of an already existing terminal.

To know the pts file just type tty command

~$ tty
/dev/pts/4

then your crontab would be:

0 07-17 * * * /home/dat/scripts/cron.out > /dev/pts/4

Another way could be to launch the program as an argument of the terminal:

xfce4-terminal --command=/home/dat/scripts/cron.out --display=:0.0 -H

where display is the X display where you want to show the terminal, -H is to tell the terminal to stay open after the command is terminated. This will create every time a new terminal.

crontab:

0 07-17 * * * /usr/bin/xfce4-terminal --display=:0.0 -H --command=/home/dat/scripts/cron.out

if the display is not present you will have an error logged by syslog.

7

Cron emails you your script's output. Unfortunately, Ubuntu does not set up local mail by default, which is why Cron tells you in the logs “No MTA installed, discarding output”.

Setting up local mail could be one way to solve your problem. Instead of a output in a terminal, you'd get an email notification.

If you want your cron job to output to a terminal, you'll have to redirect its output to the terminal. The redirection part is easy —

0 07-17 * * * /home/dat/scripts/cron.out >/dev/pts/42 2>&1

but the problem is figuring out which terminal to redirect to. There's no universal answer to that, it depends how you want to select the terminal among the ones you're logged into.

For typical uses, a GUI notification would be more appropriate. You can use notify-send. You'll need to set the DISPLAY environment variable.

0 07-17 * * * DISPLAY=:0 notify-send "$(/home/dat/scripts/cron.out)"
1
  • adding 2>&1 did the trick.
    – Rm4n
    Jan 18, 2022 at 9:27
3

Cron sends output to a mailer. If you want to see output in a terminal then you can log to a file and use tail -f to view output in the terminal you want to see output


Log to a file

  • The simplest answer is to log directly to a file with a crontab entry like:

0 07-17 * * * /home/dat/scripts/cron.out > /path/to/log.txt 2> /path/to/error.txt

Alternatives ways to log:

  • If your program is a scrip that can be written to, you could modify it to redirect output to a log file with. echo output > log.txt, or you can use a wrapper script described below.
  • If your program is a binary or otherwise un-writable, then you must write wrapper script to capture output to a file.

Example program and wrapper script:

$ cat program.sh wrapper.sh 
#!/bin/bash
# sample program
echo "arg 1=$1 arg2=$2 arg3=$3"
echo "sample error" >&2 

#!/bin/bash
# sample wrapper
exec ./program.sh "$@" >log.txt 2> error.txt

Example run 1:

$ ./wrapper.sh 1 2 3 ; cat error.txt  log.txt 
sample error
arg 1=1 arg2=2 arg3=3

Example run 2:

$ ./wrapper.sh "A B C" D E ; cat error.txt log.txt 
sample error
arg 1=A B C arg2=D arg3=E

View output in terminal:

Now that your logging both standard out and standard error to a file, in any terminal, you can run tail -f on one or both files like tail -f log.txt or tail -f log.txt error.txt so that tail will watch or rather follow the the file(s) for amendments. tail man-page

$ tail -f log.txt  error.txt
==> log.txt <==
arg 1=1 arg2=2 arg3=3

==> error.txt <==
sample error

Logging files appended afterwards:

If either log.txt or error.txt are later appended from either your program or from another terminal like $ echo "more output" >> log.txt, output is seen on the terminal running $ tail -f log.txt error.txt

==> log.txt <==
more output

Furthermore, $ echo code red >> error.txt results in:

==> error.txt <==
code red

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .