在我的应用程序中,我有以下代码:
def ifdown(iface):
rc = subprocess.run(['/sbin/ifdown', iface]).returncode
if rc:
print(f'ifdown({iface}) returned {rc}')
return False
return True
该应用程序作为 systemd 服务运行,问题是当我调用该函数时,子进程输出加倍journalctl -e -u <my_service>
:
May 10 13:42:25 evolve-4 dhclient[6319]: Killed old client process
May 10 13:42:25 evolve-4 python[1134]: Killed old client process
May 10 13:42:26 evolve-4 dhclient[6319]: Internet Systems Consortium DHCP Client 4.4.1
May 10 13:42:26 evolve-4 python[1134]: Internet Systems Consortium DHCP Client 4.4.1
May 10 13:42:26 evolve-4 python[1134]: Copyright 2004-2018 Internet Systems Consortium.
May 10 13:42:26 evolve-4 python[1134]: All rights reserved.
May 10 13:42:26 evolve-4 python[1134]: For info, please visit https://www.isc.org/software/dhcp/
May 10 13:42:26 evolve-4 dhclient[6319]: Copyright 2004-2018 Internet Systems Consortium.
May 10 13:42:26 evolve-4 python[1134]: Listening on LPF/wwan0/ee:bc:6c:be:6d:7c
May 10 13:42:26 evolve-4 python[1134]: Sending on LPF/wwan0/ee:bc:6c:be:6d:7c
May 10 13:42:26 evolve-4 python[1134]: Sending on Socket/fallback
May 10 13:42:26 evolve-4 dhclient[6319]: All rights reserved.
May 10 13:42:26 evolve-4 dhclient[6319]: For info, please visit https://www.isc.org/software/dhcp/
May 10 13:42:26 evolve-4 dhclient[6319]:
May 10 13:42:26 evolve-4 python[1134]: DHCPRELEASE of 100.68.214.247 on wwan0 to 100.68.214.248 port 67
May 10 13:42:26 evolve-4 dhclient[6319]: Listening on LPF/wwan0/ee:bc:6c:be:6d:7c
May 10 13:42:26 evolve-4 dhclient[6319]: Sending on LPF/wwan0/ee:bc:6c:be:6d:7c
May 10 13:42:26 evolve-4 dhclient[6319]: Sending on Socket/fallback
May 10 13:42:26 evolve-4 dhclient[6319]: DHCPRELEASE of 100.68.214.247 on wwan0 to 100.68.214.248 port 67
答案1
我认为这不是 python 特有的问题。我认为 dhclient 同时记录到系统日志和 stderr (或 stdout),就像在终端上运行它一样。
即,如果您将 stdout/stderr 重定向ifdown
到/dev/null
,则输出不会加倍。当然,我不知道这是否是适合您具体情况的最佳解决方案。