Solved

Unix BASH Scripting - Crontab output to file

Posted on 2011-02-22
5
602 Views
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 >
#!/usr/bin/ksh
#Alert log tidy script

logDir="/bea_domains/config/AHE_CIT/logs"
tempDir="/tmp/alertLogArchive"
date=`date +%Y%m%d`
archiveDir="/bea_domains/config/AHE_CIT/logs/archive"

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

0
Comment
Question by:Lico_w
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
Comment Utility
Hi,

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

wmp
0
 

Assisted Solution

by:plshrk
plshrk earned 250 total points
Comment Utility
The output usually is in your mail que.
Try this:  0 * * * * ~/script.sh >/dev/null 2>&1
0
 

Author Closing Comment

by:Lico_w
Comment Utility
Both solutions work perfectly.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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.

wmp
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
Note that

cmd 2>&1 >file

will not produce the same results as

cmd >file 2>&1

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now