How to read /dev/log?

偶尔善良 提交于 2019-12-23 13:15:45

问题


I would like to directly access to syslog messages from Python by reading /dev/log.

My (very limited) understanding is that the correct way is to read from there is to bind a datagram socket.

import socket

sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.bind('/dev/log')
sock.listen(1)
while True:
    data, addr = sock.recvfrom(1024)
    print(data)

Apparently /dev/log is in use:

Traceback (most recent call last):
  File "readlog.py", line 4, in <module>
    sock.bind('/dev/log')
OSError: [Errno 98] Address already in use

How should I read /dev/log from Python?


EDIT: per @Barmar's comment - only one process can access /dev/log so that part is clear, the device must be clean before reading from it. sudo lsof /dev/log does not show anything.

A answer in a Java thread around this subject mentioned that syslog should be shut down before. I also tried that, lsof | grep "/dev/log" was empty but I got the error nevertheless.
Isn't it possible to have several processes reading from /dev/log?

来源:https://stackoverflow.com/questions/40787610/how-to-read-dev-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!