我正在尝试从 Python 中的 journalctl 读取系统日志的最后 600 秒。从命令行中,这工作:
~ $ 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.
但是,当我尝试使用子进程在 python 中执行此操作时,我得到:
~ $ 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'
''
>>>
那么,这是怎么回事呢?我错过了什么?
那是因为你引用了两次你的时间:
"'600 seconds ago'"
因此,删除这样的内部引号:
"600 seconds ago"