Unix BASH Scripting - Crontab output to file

Posted on 2011-02-22
Last Modified: 2012-05-11
I have a script which runs weekly on my crontab (see below) but would like to output results to file, how would I do this?

I tried including this option pointing to a txt file but didn't seem to work:

2>&1 >
#Alert log tidy script

date=`date +%Y%m%d`

mkdir $tempDir

cd $logDir

find . -name "*alert.log*" -type f -mtime +30 -exec mv '{}' $tempDir \;

tar cvf - ${tempDir}/* | gzip > ${archiveDir}/alertlog_archive.${date}.tar.gz

rm -fr $tempDir

cd $archiveDir

find . -name "alertlog_archive*" -type f -mtime +60 -exec rm {} \;

Open in new window

Question by:Lico_w
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 68

Accepted Solution

woolmilkporc earned 250 total points
ID: 34950528

you could change yiur crontab entry like

* * * * * /path/to/script >/path/to/log 2>/path/to/errorlog

or you could add as the second/third lines to your script

exec 1>/path/to/log
exec 2>/path/to/errorlog

Of course you could combine both logs by using

2>&1 instead of 2>/path/to/errorlog


Assisted Solution

plshrk earned 250 total points
ID: 34950585
The output usually is in your mail que.
Try this:  0 * * * * ~/ >/dev/null 2>&1

Author Closing Comment

ID: 34951744
Both solutions work perfectly.
LVL 68

Expert Comment

ID: 34951768
The second solution will give no output at all and you won't receive any email either. Is this what you desire?

Thx for the points anyway.

LVL 48

Expert Comment

ID: 34955234
Note that

cmd 2>&1 >file

will not produce the same results as

cmd >file 2>&1


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. Please see for the updated article. It is avail…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question