Solved

Unix BASH Scripting - Crontab output to file

Posted on 2011-02-22
5
610 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
[X]
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
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 34950528
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
ID: 34950585
The output usually is in your mail que.
Try this:  0 * * * * ~/script.sh >/dev/null 2>&1
0
 

Author Closing Comment

by:Lico_w
ID: 34951744
Both solutions work perfectly.
0
 
LVL 68

Expert Comment

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

wmp
0
 
LVL 48

Expert Comment

by:Tintin
ID: 34955234
Note that

cmd 2>&1 >file

will not produce the same results as

cmd >file 2>&1

0

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