当程序作为系统用户从系统服务运行时,有时记录失败。因为root总是有效的

时间:2017-12-08 18:09:11

标签: python logging systemd

我有一个名为user的系统用户帐户,如下所示:

useradd -r user

我有一个非常简单的Python程序,它只记录一行:

#!/usr/bin/env python
import logging
import os

# Set logging options
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))

logging.info('LOG SOMETHING')

我有一个systemd服务来运行Python程序:

[Unit]
Description=Run simple.py

[Service]
ExecStart=/usr/local/bin/simple.py
User=user

如果我从命令行运行Python程序,同时使用rootuser个用户,它的工作正常:

# /usr/local/bin/simple.py 
INFO:root:LOG SOMETHING
# runuser -l user -c '/usr/local/bin/simple.py' 
runuser: warning: cannot change directory to /home/user: No such file or directory
INFO:root:LOG SOMETHING

忽略该警告。那是因为系统用户帐户user没有主目录。

现在,如果我以systemd用户身份运行user服务(请注意服务文件末尾的User=user),则日志记录有时会失败,有时不会失败:

# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# journalctl -u simple.service | tail -18
Dec 08 17:57:23 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:23 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:24 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:24 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:25 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:25 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:25 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:25 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:26 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:26 localhost simple.py[12774]: INFO:root:LOG SOMETHING
Dec 08 17:57:26 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:27 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:27 localhost systemd[1]: Started Run simple.py.

现在,如果我以systemd用户身份运行root服务(只删除服务文件末尾的User=user,当然还有systemctl daemon-reload} ,记录始终有效:

# systemctl start simple.service
# journalctl -u simple.service | tail -18
Dec 08 18:04:03 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:03 localhost simple.py[12827]: INFO:root:LOG SOMETHING
Dec 08 18:04:03 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:03 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:03 localhost simple.py[12835]: INFO:root:LOG SOMETHING
Dec 08 18:04:03 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:04 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:04 localhost simple.py[12843]: INFO:root:LOG SOMETHING
Dec 08 18:04:04 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:05 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:05 localhost simple.py[12851]: INFO:root:LOG SOMETHING
Dec 08 18:04:05 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:05 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:05 localhost simple.py[12859]: INFO:root:LOG SOMETHING
Dec 08 18:04:05 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:06 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:06 localhost simple.py[12867]: INFO:root:LOG SOMETHING
Dec 08 18:04:06 localhost systemd[1]: Started Run simple.py.

为什么?

0 个答案:

没有答案