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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

726 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