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