I'd like to be able to run a UNIX command precisely every second over a long time period.
I need a solution, which does not lag behind after a certain time, because of the time the command itself needs for execution. sleep, watch, and a certain python script all failed me in this regard.
On the microcontroller's such as the http://Arduino.cc I'd do that through hardware clock interrupts. I'd like to know whether there is a similar time-precise shell script solution. All the solutions which I found within StackExchange.com, resulted in a noticeable time lag, if run over hours. See details below.
Practical purpose / application
I want to test wether my network connection is continuously up by sending timestamps via
nc (netcat) every 1 second.
precise-timestamp-generator | tee netcat-sender.txt | nc $receiver $port
nc -l -p $port > netcat-receiver.txt
After completion, compare the two logs:
diff netcat-sender.txt netcat-receiver.txt
The diffs would be the untransmitted timestamps. From this I would know at what time my LAN / WAN / ISP makes troubles.
while [ true ]; do date "+%Y-%m-%d %H:%M:%S" ; sleep 1; done | tee timelog-sleep.txt
Gets a certain offset over time, as the command within the loop also takes a little time.
cat timelog-sleep.txt 2012-07-16 00:45:16 [...] 2012-07-16 10:20:36
Seconds elapsed: 34520
wc -l timelog-sleep.txt
Lines in file: 34243
- 34520-34243 = 277 timing problems
- 34520/34243 = 1.008 = 0.8 % off
Solution REPEAT PYTHON
repeat.py 1 "date '+%Y-%m-%d %H:%M:%S'" >> timelog-repeat-py.txt
Supposed to avoid the time offset, but fails to do so.
wc -l timelog-repeat-py.txt 2012-07-16 13:42:44 [...] 2012-07-16 16:45:24
Seconds elapsed: 10960
wc -l timelog-repeat-py.txt
Lines in file: 10859
- 10960-10859 = 101 timing problems
- 10960/10859 = 1.009 = 0.9 % off
watch -n 1 "date '+%Y-%m-%d %H:%M:%S' >> ~/Desktop/timelog-watch.txt"
wc -l timelog-watch.txt 2012-07-16 11:04:08 [...] 2012-07-16 13:25:47
Seconds elapsed: 8499
wc -l timelog-watch.txt
Lines in file: 8366
- 8499-8366 = 133 timing problems.
- 8499/8366 = 1.016 = 1.6 % off.