TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: ADRIAN
from: DRUCK
date: 2018-12-07 19:51:00
subject: Re: Odd date behaviour

On 07/12/2018 14:43, Adrian wrote:
> Raspbrian 4.9.59-v7
> 
> I'm seeing some odd behaviour with date in some stuff that a Pi is running.
> 
> I've got a cron job that runs every minute, calling a bash script.  The 
> script writes out to a log file every time it runs 

How are you writing to the file? Simple redirection of stdout?

> giving the state of a 
> sensor, typically four or five lines of output, and each stage starts 
> with the time, generated by :
> 
> date +%H%M
> 
> If there is a change in the state of the sensor, then this will be 
> reported, using just the date command :
> 
> echo `date` 

Better to use:-

     echo $(date) 

It's more reliable then back ticks which can have strange issues in 
scripts.
> The strange thing is that the entries published every minute are 
> accurate, but in the change of state entries can be out by anything up
> to 9 minutes late, the discrepancy varies.

I'm not quite sure from your description if you mean the date in the log
messages is wrong, or if the log entry is being put in the log file 
after entries which should precede it.

One thing you might want to consider is instead of doing your own 
echoing of dates and messages to a file, is to use syslog, which does 
all this for you.

You can either output to syslog explicitly from your bash script

     echo "state change message" | logger -t TAG

Or redirect the entire output of the script by doing it in he crontab

     /full/path/to/your/script | /usr/bin/logger -t TAG

TAG is what will appear in each line in syslog so you can use something 
appropriate to your program such as SensorState.

To get your messgaes put in thier own logfile rather than 
/var/log/syslog create a in /etc/rsyslog.d/ with a name such as 
SensorState.conf containing:-

     # Filter SensorState tags
     :syslogtag, isequal, "SensorState:" {
     /var/log/SensorState.log
     stop
     }

Finally to stop your log file growing indefinitely you can register it 
with log rotate. Create /etc/logrotate.d/SensorState containing:-

     /var/log/backup2NASimage.log {
         weekly
         rotate 4
         missingok
         notifempty
         compress
         nocreate
     }

This will start a new log file every week, keeping compressed copies of 
the last 4.

---druck

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.