使用 Python 子进程读取 journalctl 输出
Reading journalctl output using Python subprocess
我正在尝试从 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"
我正在尝试从 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"