I am trying to emulate this shell command in Python using the systemd libraries http://www.freedesktop.org/software/systemd/python-systemd/journal.html
I am actually trying to emulate this command but within Python.
journalctl --since=-5m --no-pager
I have seen others do this in Python by calling the journal executable but that is a pretty bad way of doing it.
I wrote this simple script based on the documentation linked above
import select
from systemd import journal
j = journal.Reader()
j.log_level(journal.LOG_INFO)
# j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
j.seek_tail()
j.get_next()
while j.get_next():
for entry in j:
if entry['MESSAGE'] != "":
print(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])
There are a few issues here
- the log seems to start from about 5 days ago which means the seek_tail did not appear to work.
- I am getting a lot of junk in here is there a specific filter I should use to match the data I get from journalctl command given at the beginning of the question?
Ideally longer term I want to just be following this journal based on a set of filters/matches to emulate the command 'journalctl -f' but I just need to resolve this issue first. I want to end up with something like this but it doesnt work either.
import select
from systemd import journal
j = journal.Reader()
j.log_level(journal.LOG_INFO)
# j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
j.seek_tail()
p = select.poll()
p.register(j, j.get_events())
while p.poll():
while j.get_next():
for entry in j:
if entry['MESSAGE'] != "":
print(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])