脚本(bash、python 等)内的日志和打印如何与单元名称关联?
假设我有一个非常简单的单元文件 test-service.service
[Unit]
Description=Test Unit
[Service]
Type=simple
User=myuser
Group=myuser
ExecStart=/usr/bin/bash /home/myuser/hello.sh
[Install]
WantedBy=default.target
hello.sh 包含:
#!/bin/bash
echo "Hello World"
当我尝试显示该特定单元文件的日志时,我看不到日志行“Hello World”:
journalctl -u test-service -f
-- Logs begin at Mon 2019-12-09 10:07:15 CET. -- Dec 09 10:10:51 localhost.localdomain systemd[1]: Started Test Unit.
当我发出journalctl而不按单位过滤时,我可以看到日志行:
journalctl -f
-- Logs begin at Mon 2019-12-09 10:07:15 CET. --
Dec 09 10:14:07 localhost.localdomain bash[4776]: Hello World
现在这个单元文件也会发生同样的事情:
[Unit]
Description=Test Unit
[Service]
Type=simple
User=myuser
Group=myuser
ExecStart=/usr/bin/python /home/myuser/test.py
[Install]
WantedBy=default.target
test.py 包含:
import logging
class TestService(object):
def __init__(self):
logging.basicConfig()
self.log = logging.getLogger("test-service")
self.log.setLevel(logging.INFO)
def hello_world(self):
exit_code = 0
print("Hello world, exit_code = {}".format(exit_code))
self.log.info("Hello world!!!")
exit(exit_code)
if __name__ == "__main__":
print("starting up..")
r = TestService()
r.hello_world()
当我再次尝试显示该特定单元文件的日志时,我没有看到日志行“Hello World”:
journalctl -u test-service -f
-- Logs begin at Mon 2019-12-09 10:07:15 CET. --
Dec 09 10:10:51 localhost.localdomain systemd[1]: Started Test Unit.
当我发出journalctl而不按单位过滤时,我可以看到日志行:s
systemd[1]: Started Test Unit.
Dec 09 10:28:52 localhost.localdomain python[5449]: INFO:test-service:Hello world!!!
Dec 09 10:28:52 localhost.localdomain python[5449]: starting up..
Dec 09 10:28:52 localhost.localdomain python[5449]: Hello world, exit_code = 0
bash 和 python 脚本中的脚本如何与单元文件关联? “bash[4776]”和“python[5449]”是脚本关联的默认单元名称还是默认值是什么?