Re: leading null characters produced in script...
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Mon, 30 Oct 2006 02:17:31 -0500
On 2006-10-30, Avalon1178 wrote:
Hello,
I'm trying to write a small script using bash that essentially archives
logs to prevent logs from getting too big and take up drive space. The
way I do it, when a log file reaches a certain size, I have an external
script that copies that log file to another file, truncates to 0 length
the original log file, and let the the other script that generates the
log continues on. The problem I'm having is I'm getting leading null
characters at the beginning of the new log, which sometimes takes too
many bytes which triggers my log archiver prematurely.
For example, here's is my (simplified) log archiver script written in
bash:
while [1]; do
if [ -a $logFile ]; then
logSize=$(stat -c%s $logFile);
if [ $logSize -ge 5000 ]; then
cp $logFile $logFile.arch
cat /dev/null >$logFile
fi
fi
done
Once the $logFile reaches 5000 bytes, it copies it to $logFile.arch and
the other script will continue to populate $logFile once more. When
$logFile once again reaches 5000 bytes, the archiving is triggered.
The problem is, when $logFile is written by this other script the
second time, a bunch of leading nulls are created...and its usually so
long that it triggers the archiving prematurely. (When I do a 'od -h
logFile | less' on the archived log, I basically see hex chars of 0000
0000 * until the first non-null character)
Here is how the other script is generating the $logFile:
...
./otherScript >$logFile 2>&1 &
...
Is there anything I'm doing wrong? Has anyone encountered seeing
leading null characters? If there's no way in getting around this,
what is the best way (preferably in bash) to get rid of these leading
null characters?
Your otherScript doesn't close the file, so it continues to write
at the position it was previously at.
The easiest solution is to move the redirection inside the script,
so that it will open and close the file whenever it needs to write
to the it, e.g.:
logfile=$1
exec 2>&1
while :
do
: whatever needs doing
printf "%s\n" result >> "$logfile"
done
Rather than:
while :
do
: whatever needs doing
printf "%s\n" result
done > "$logfile"
Call the first version with:
../otherScript "$logfile"
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
.
- Follow-Ups:
- Re: leading null characters produced in script...
- From: Avalon1178
- Re: leading null characters produced in script...
- References:
- leading null characters produced in script...
- From: Avalon1178
- leading null characters produced in script...
- Prev by Date: Re: CPU Load average VS Idle %
- Next by Date: Re: Problem in MySQL
- Previous by thread: Re: leading null characters produced in script...
- Next by thread: Re: leading null characters produced in script...
- Index(es):
Relevant Pages
|