Skip to content

Instantly share code, notes, and snippets.

@morbeo
Last active November 11, 2021 11:31
Show Gist options
  • Save morbeo/26de548bb6be7bff407e689002236645 to your computer and use it in GitHub Desktop.
Save morbeo/26de548bb6be7bff407e689002236645 to your computer and use it in GitHub Desktop.
tailsince [N minutes] <file> [file..]
#!/usr/bin/env bash
period=$1
shift
for file in $@; do
timeshift=$(date -d"-${period} minutes" +%s);
while read -u3 date time line; do
epoch_timestamp=$(date -d"${date} ${time}" +%s);
if [[ ${timeshift} -lt ${epoch_timestamp} ]]; then
echo "${date} ${time} ${line}";
else
break;
fi;
done 3< <(tac ${file}) | tac
shift
if [[ -n $1 ]]; then echo -- "----- $1 -----"; fi
done
@morbeo
Copy link
Author

morbeo commented Nov 11, 2021

Example and comparison with tail 10

root@xxx:~# tail -10 /var/log/perp/nginx-reloader/current
2021-11-11 08:28:11.157283 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:29:00.554541 nginx: ok
2021-11-11 08:29:00.787734 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:29:00.788908 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:30:44.169298 nginx: ok
2021-11-11 08:30:44.551715 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:30:44.552826 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:33:55.977909 nginx: ok
2021-11-11 08:33:56.268204 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:33:56.269427 nginx: configuration file /etc/nginx/nginx.conf test is successful

root@xxx: ~# tailsince 10 /var/log/perp/nginx-reloader/current
2021-11-11 08:24:50.490544 nginx: ok
2021-11-11 08:24:50.776741 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:24:50.777936 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:26:35.376918 nginx: ok
2021-11-11 08:26:35.698485 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:26:35.699742 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:27:24.143115 nginx: ok
2021-11-11 08:27:24.364960 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:27:24.365868 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:28:10.814723 nginx: ok
2021-11-11 08:28:11.156522 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:28:11.157283 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:29:00.554541 nginx: ok
2021-11-11 08:29:00.787734 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:29:00.788908 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:30:44.169298 nginx: ok
2021-11-11 08:30:44.551715 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:30:44.552826 nginx: configuration file /etc/nginx/nginx.conf test is successful
2021-11-11 08:33:55.977909 nginx: ok
2021-11-11 08:33:56.268204 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2021-11-11 08:33:56.269427 nginx: configuration file /etc/nginx/nginx.conf test is successful

@morbeo
Copy link
Author

morbeo commented Nov 11, 2021

Currently with perp log format

2021-11-11 08:24:50.490544 nginx: ok
^--$date-^ ^-----$time---^ ^-$line->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment