Getting output from a cron job on the terminal
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.
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)"
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