【问题标题】:Linux output redirection daemon does not workLinux 输出重定向守护进程不起作用
【发布时间】:2017-10-17 23:05:07
【问题描述】:

我有一个这样的 python 脚本:

while True:
    print("ok")

我创建了一个守护进程 test.service:

[Unit]
Description=Un service de test.

[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/andy/Desktop/test.py > /home/andy/Desktop/test.log 2>&1

[Install]
WantedBy=multi-user.target

我使用 sudo systemctl start test.service 激活它。 没有创建日志文件:输出重定向不起作用。

当我写 systemctl status test.service 时,我有:

● test.service - Un service de test.
   Loaded: loaded (/etc/systemd/system/test.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2017-10-18 00:50:29 CEST; 20s ago
 Main PID: 3651 (python3)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/test.service
           └─3651 /usr/bin/python3 /home/andy/Desktop/test.py > /home/andy/Desktop/test.log 2>&1

oct. 18 00:50:29 andy-G551JW systemd[1]: Started Un service de test..
oct. 18 00:50:29 andy-G551JW python3[3651]: ok
oct. 18 00:50:29 andy-G551JW python3[3651]: ok

如何在守护程序脚本中重定向输出?

【问题讨论】:

标签: python linux output daemon


【解决方案1】:

不要重定向它的输出。让脚本打印到标准输出。 Systemd 会将其输出存储在 systemd 日志中,您可以使用 journalctl -u testsystemctl status test 查看。

【讨论】:

  • 我只有 2 个“ok”和一段时间 True:print("ok")。正常吗?
猜你喜欢
  • 2014-12-05
  • 1970-01-01
  • 2012-08-07
  • 2011-03-06
  • 2014-11-24
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多