LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-22-2009, 03:35 AM   #1
The Pearls Master
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Rep: Reputation: 0
Standard output redirection doesn't work correctly with cron job


Hi to everybody! I'm new in this forum ( ) and this is my first post.

I've written a small shell script to delete old backup file on a server with Linux CentOS; all backup files are named like that:

backup_YYYYMMDD.tar.gz

where YYYY= Year, MM= Month, DD=Day

The script outputs some info in a log file. If I execute manually the script from the shell everything works well, but when cron job executes it only some info are correctly logged into the log file and i can't understand why.

I've tried some search in this forum and I read that cron manages stdin and stdout in a different way, but still I couldn't find an answer. :-(

Here is the code:

Code:
#setting input vars
backup_dir=/var/backups/
backup_days=30

# setting internal vars
curdate_string=$(date +%Y%m%d)_$(date +%H%M)
log_file=$0_$curdate_string.log
actual_timestamp=$(date +%s)
backup_days_seconds=$(($backup_days*24*60*60))

#deleting old backups
echo "--------------------------------------------------------------------------------" >> $log_file
echo OLD BACKUP DELETE >> $log_file
for current_backupname in $(ls -l *.gz | awk '{ print $9 }' | grep '[0-9]\{8\}')
do
	echo "-->" Processing file \"$backup_dir$current_backupname\" >> $log_file
	current_backupdate=`echo $current_backupname | grep -o '[0-9]\{8\}'`
	current_timestamp=`date -d$current_backupdate +%s`
	
	if [ $(($actual_timestamp-$current_timestamp)) -gt "$backup_days_seconds" ]
	then
		rm -f $backup_dir$current_backupname
		echo Backup file DELETED >> $log_file
	else
		echo Backup file keeped >> $log_file
	fi
done
When cron executes the script only the first two "echo" are correctly redirected in the $log_file and i can't understand why. :-(

Thanks very much to everybody. :-)
 
Old 07-22-2009, 08:03 AM   #2
The Pearls Master
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Original Poster
Rep: Reputation: 0
I've just found the error: it was my fault. Nothing wrong with cron job, i simply forgot to add the base-folder in the "for" statement.

I've fixed these two lines:

Code:
for current_backupfile in $(ls -l $backup_dir*.gz | awk '{ print $9 }' | grep '[0-9]\{8\}')
Code:
rm -f $current_backupfile
Now everything works correctly!

Thanks to all the people that read my post!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why my cron job doesn't work byng08 Linux - Newbie 7 11-26-2008 01:17 AM
Way To Have Output Of Cron Job Emailed jmoschetti45 Linux - General 1 05-22-2007 01:42 AM
cron job mailing blank output rsmccain Linux - General 4 10-09-2006 04:00 PM
How do I get a cron job to work? socceroos Linux - Software 2 03-23-2006 04:22 PM
Problem displaying program output when driven by a cron job garmon Linux - Newbie 1 07-22-2004 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration