Check if file is not older than 2 seconds in script

How can i check in a script if a file which will be created by my script is younger (or not older) than 2 seconds?

Background:
I run one script on two hostapd_cli instances (wl0-ap0, wl1-ap0) to identify precense. Sometimes wifi equipment is changing the wlan interface:

[1743299787.681] daemon.notice hostapd: wl0-ap0: AP-STA-DISCONNECTED xx:xx:xx:xx:xx:xx
[1743299787.728] daemon.notice hostapd: wl1-ap0: AP-STA-CONNECTED xx:xx:xx:xx:xx:xx

I dont want to get the disconnect state, if its connected after 50ms again.

The first call of the script creates a state file, the second call of the script should test if the file is not older than 2 seconds.

What i'm using now is this:

oldfile=$(find /tmp/tmp/$3.state -mmin +1 2> /dev/null)
if [ ! -z "$oldfile" ]; then
  rm /tmp/tmp/$3.state
fi

Problem is, that the file can be over a minute old before it gets deleted...

Get the timestamp of the file with date -r /tmp/tmp/$3.state +%s and subtract it from current time date +%s.

3 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.