Re: leading null characters produced in script...



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
.



Relevant Pages

  • Re: Expect: spawned process in sleep state (log_file problem?)
    ... I can't imagine why it has anything to do with the logfile. ... script is hanging and output of truss would be helpful. ... > the xyz script ran in background. ... > want xyz to have its own log file. ...
    (comp.lang.tcl)
  • Re: leading null characters produced in script...
    ... | I'm trying to write a small script using bash that essentially archives ... | way I do it, when a log file reaches a certain size, I have an external ... | many bytes which triggers my log archiver prematurely. ...
    (comp.os.linux.misc)
  • script watching tail -f output
    ... I want to run a shell script which starts the application, ... monitors the end of the log file for a new entry of the line ... The script works when the logfile is being written into slowly by the ...
    (comp.unix.shell)
  • script watching tail -f output
    ... I want to run a shell script which starts the application, ... monitors the end of the log file for a new entry of the line ... The script works when the logfile is being written into slowly by the ...
    (comp.unix.shell)
  • Re: leading null characters produced in script...
    ... | I'm trying to write a small script using bash that essentially archives ... | way I do it, when a log file reaches a certain size, I have an external ... | many bytes which triggers my log archiver prematurely. ...
    (comp.os.linux.misc)