Re: leading null characters produced in script...
- From: Unruh <unruh-spam@xxxxxxxxxxxxxx>
- Date: 30 Oct 2006 20:49:46 GMT
"Avalon1178" <Avalon1178@xxxxxxx> writes:
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?
Yes, your first program has the log file open ( the redirection) when you
empty the log file, the first program is still paged to a certain location
in the log file and everything gets confused.
You need to close the log file (stop the otherScript) move the log file and
then open it again. changing a file with a second program while a first
one is writing to it is not a good idea.
.
- References:
- leading null characters produced in script...
- From: Avalon1178
- leading null characters produced in script...
- Prev by Date: Hashing out tyoed passwords
- Next by Date: Re: leading null characters produced in script...
- Previous by thread: Re: leading null characters produced in script...
- Next by thread: More than 2 Gb of swap
- Index(es):
Relevant Pages
|