Re: leading null characters produced in script...



"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.


.



Relevant Pages

  • Re: Loading mulitple files
    ... I removed logPath and included it as a Const for logFile. ... I now notify the admin with a result status and where the log file was ... dim logXML, logtext, arrLogs, arrText ... set att = logXML.createAttribute ...
    (microsoft.public.scripting.vbscript)
  • Re: Need Some Quick SBS/Exchange Help!
    ... This logfile has been damaged and is unusable. ... log-file fill pattern first appeared in sector 5452. ... Is there an antivirus program on this box that does not have the Exchange folders excluded? ... check the quarantined folder for the ..e00 log file. ...
    (microsoft.public.windows.server.sbs)
  • Re: Delete top part of file
    ... gzip logfile.1 ... If logfile is still being written to, ... don't want the/a log file to be missing at any time (e.g. avoid race ... typically preferable not to lose current log data as the rotation is ...
    (comp.unix.shell)
  • Re: Logfile
    ... The issue is that I just want to find a way to fill up the log file ... with messages generated as the process flows. ... In the procedure the message is written to the logfile and added with a ...
    (comp.databases.ms-access)
  • 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)