pythonsubprocessjournal

Reading journalctl output using Python subprocess


I'm trying to read the last 600 seconds of the systemlog from journalctl in Python. From the command-line this works:

~ $ journalctl --since='600 seconds ago' --no-pager -p6..6
-- Logs begin at Sat 2016-05-28 09:43:08 CEST, end at Sun 2016-05-29 10:43:30 CEST. --
May 29 10:41:57 rbian dhclient[234]: DHCPREQUEST on eth0 to 10.0.1.2 port 67
May 29 10:41:57 rbian dhclient[234]: DHCPACK from 10.0.1.2
May 29 10:41:57 rbian dhclient[234]: bound to 10.0.1.11 -- renewal in 3177 seconds.

However, when I try to do that in python using subprocess I get this:

~ $ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.Popen(["journalctl", "--since", "'600 seconds ago'", "--no-pager", "-p", "6..6"], stdout=subprocess.PIPE).stdout.read()
Failed to parse timestamp: '600 seconds ago'
''
>>>

So, what's up with that? What am I missing?


Solution

  • That is because you quoted your time for twice:

    "'600 seconds ago'"
    

    so, remove internal quotes like this:

    "600 seconds ago"