Ads

how to view last 1 hour log in Linux ?

in Bash script or Linux terminal

%%bash

journalctl --since "1 hour ago"

in Python

%%python3

from datetime import datetime, timedelta

from re import match, compile

tme = [(datetime.now() - timedelta(minutes=i))

    .strftime("%b %e %H:%M") for i in range(60)]

compose = compile('^[A-Za-z]{3}\s{2}\d{1,2}\s\d{1,2};\d{1,2}')

with open('/var/log/syslog') as log:

    for line in log:

        if compose.match(line).group() in tme:

            print(line.strip())


If you want to get new posts, make sure to subscribe to Value In Brief by Email.