将 systemd StandardOutput 定向到文件时如何包含日期+时间

Sys*_*ral 6 systemd

我有一个 systemd 服务,它输出到日志文件

StandardOutput=file:/path/to/log/file
StandardError=file:/path/to/error/file
Run Code Online (Sandbox Code Playgroud)

我怎样才能在这些输出和错误前面添加数据+时间,以便它读取

10-09-2019 12:43:23 The output here.
Run Code Online (Sandbox Code Playgroud)

Sea*_*mus 0

试试这个(警告:未经测试)

  1. EnableStart一个“logging.service”systemctl

  2. [Service]定义中logging.service使用以下内容:

[Unit]
Description=Start logging server   # or whatever you choose to call it

[Service]
...
Type=forking
ExecStart=/bin/bash myLogger.sh start
...

Run Code Online (Sandbox Code Playgroud)
  1. 编写bash脚本myLogger.sh(或者无论你怎么称呼它)。在脚本中,使用以下命令开始日志中的每一行:
printf '%(%Y-%m-%d %H:%M:%S)T' -1
Run Code Online (Sandbox Code Playgroud)

printf不会引入换行符,因此您应该能够在此之后立即附加日志数据。