如何在journalctl中显示来自systemd-timer脚本的回显消息?

如何在journalctl中显示来自systemd-timer脚本的回显消息?

我通过 systemd 计时器运行一个 shell 脚本,该脚本启动一些程序(它们显示一些输出)并用于echo提供一些反馈。

root@srv ~ [127]# cat /etc/systemd/system/borg.timer
[Unit]
Description=runs borg every hour

[Timer]
OnCalendar=hourly
Persistent=true

[Install]
WantedBy=multi-user.target

root@srv ~# cat /etc/systemd/system/borg.service
[Unit]
Description=borg backup

[Service]
Type=simple
ExecStart=/etc/local/borg.sh
root@srv ~# cat /etc/local/borg.sh
#!/bin/bash

echo "→→→ starting borg backup at $(date)"

# it is OK to move repositories
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
(...)
  • 以交互方式启动脚本时,我echo通过 看到两个输出(程序和输出)journalctl

  • 当通过计时器运行时,仅显示程序输出。

我知道后者是非交互式运行,但由于程序输出有人进入,journalctl那么定时器又如何呢?

有没有办法让脚本中出现这样的反馈输出?


systemctl下面是通过和journalctl临时启动脚本时通过输出的内容的比较sytemctl start borg。我现在想知道这是一个“两阶段”脚本(bash运行borg.sh本身python在幕后运行)是否可能是过滤输出的原因(只是一个疯狂的猜测)

输出来自systemctl

root@srv ~# systemctl status borg
● borg.service - borg backup
     Loaded: loaded (/etc/systemd/system/borg.service; static)
     Active: active (running) since Mon 2022-08-08 18:31:54 CEST; 1min 22s ago
TriggeredBy: ● borg.timer
   Main PID: 2651152 (borg.sh)
      Tasks: 2 (limit: 28688)
     Memory: 1.1G
        CPU: 54.304s
     CGroup: /system.slice/borg.service
             ├─2651152 /bin/bash /etc/local/borg.sh
             └─2651737 /usr/bin/python /usr/bin/borg compact /backup/borg/srv

Aug 08 18:32:46 srv borg.sh[2651687]: Keeping archive (rule: weekly #4):       231d5f07-3453-46a5-a466-b8760196e120 Sun, 2022-07-03 23:00:11 [ab22320beda4d0f4ac247686ad>
Aug 08 18:32:46 srv borg.sh[2651687]: Keeping archive (rule: monthly #1):      3b280578-586f-4f62-80c1-b296aa8982c2 Thu, 2022-06-30 23:00:10 [067e23be4b7e33ec7b87f32603>
Aug 08 18:32:48 srv borg.sh[2651687]: ------------------------------------------------------------------------------
Aug 08 18:32:48 srv borg.sh[2651687]:                        Original size      Compressed size    Deduplicated size
Aug 08 18:32:48 srv borg.sh[2651687]: Deleted data:               -3.54 GB             -1.61 GB             -1.33 MB
Aug 08 18:32:48 srv borg.sh[2651687]: All archives:                2.23 TB              1.82 TB            104.85 GB
Aug 08 18:32:48 srv borg.sh[2651687]:                        Unique chunks         Total chunks
Aug 08 18:32:48 srv borg.sh[2651687]: Chunk index:                  199025              7333955
Aug 08 18:32:48 srv borg.sh[2651687]: ------------------------------------------------------------------------------
Aug 08 18:32:48 srv borg.sh[2651152]: →→→ compact backup for /backup/borg/srv
Aug 08 18:33:28 srv borg.sh[2651152]: repo: /backup/borg/srv id: 2022-08-08T18:32:00+02:00 hash:
Aug 08 18:33:30 srv borg.sh[2651152]: →→→ ended backup on srv
Aug 08 18:33:30 srv borg.sh[2651152]: →→→ starting backup on outscale
Aug 08 18:33:30 srv borg.sh[2651152]: →→→ create backup for [email protected]:/backup/srv
Aug 08 18:33:32 srv borg.sh[2652145]: M /etc/docker/domotique/data/hass/.storage/hacs.repositories
Aug 08 18:33:32 srv borg.sh[2652145]: M /etc/docker/domotique/data/hass/.storage/auth

相同的日志行通过journalctl

Aug 08 18:32:46 srv borg.sh[2651687]: Keeping archive (rule: weekly #4):       231d5f07-3453-46a5-a466-b8760196e120 Sun, 2022-07-03 23:00:11 [ab22320beda4d0f4ac247686adb39bd64533d93c9b02172e4299bf5f7e8ceee8]
Aug 08 18:32:46 srv borg.sh[2651687]: Keeping archive (rule: monthly #1):      3b280578-586f-4f62-80c1-b296aa8982c2 Thu, 2022-06-30 23:00:10 [067e23be4b7e33ec7b87f32603d5a05aa5e1f77e6dff17451b3d63db9130cafc]
Aug 08 18:32:48 srv borg.sh[2651687]: ------------------------------------------------------------------------------
Aug 08 18:32:48 srv borg.sh[2651687]:                        Original size      Compressed size    Deduplicated size
Aug 08 18:32:48 srv borg.sh[2651687]: Deleted data:               -3.54 GB             -1.61 GB             -1.33 MB
Aug 08 18:32:48 srv borg.sh[2651687]: All archives:                2.23 TB              1.82 TB            104.85 GB
Aug 08 18:32:48 srv borg.sh[2651687]:                        Unique chunks         Total chunks
Aug 08 18:32:48 srv borg.sh[2651687]: Chunk index:                  199025              7333955
Aug 08 18:32:48 srv borg.sh[2651687]: ------------------------------------------------------------------------------
Aug 08 18:33:32 srv borg.sh[2652145]: M /etc/docker/domotique/data/hass/.storage/hacs.repositories
Aug 08 18:33:32 srv borg.sh[2652145]: M /etc/docker/domotique/data/hass/.storage/auth

→→→以(以及我忘记添加前缀的行)开头的所有内容repo都来自echo.

答案1

我找到了如何显示线条,但不完全理解其中的区别:

root@srv ~ [SIGINT]# journalctl -u borg -f
Aug 08 18:34:59 srv borg.sh[2652992]: All archives:                1.10 TB            806.57 GB             23.34 GB
Aug 08 18:34:59 srv borg.sh[2652992]:                        Unique chunks         Total chunks
Aug 08 18:34:59 srv borg.sh[2652992]: Chunk index:                  104061              5351569
Aug 08 18:34:59 srv borg.sh[2652992]: ------------------------------------------------------------------------------
Aug 08 18:34:59 srv borg.sh[2651152]: →→→ compact backup for [email protected]:/backup/srv
Aug 08 18:35:01 srv borg.sh[2651152]: repo: [email protected]:/backup/srv id: 2022-08-08T18:33:30+02:00 hash:
Aug 08 18:35:03 srv borg.sh[2651152]: →→→ ended backup on outscale
Aug 08 18:35:04 srv borg.sh[2651152]: →→→ ended borg backup at Mon Aug  8 18:35:04 CEST 2022

我以前使用过

root@srv ~ [SIGINT]# journalctl _COMM=borg -f
Aug 08 18:34:58 srv borg.sh[2652992]: Keeping archive (rule: daily #3):        16cccd33-5293-4a5f-8d36-35a46bfbe3b4 Wed, 2022-08-03 23:00:47 [5ae28fbaef906725950b9b672935c5c1ff9ebc8987c1fb8d96b3db65bbd6e375]
Aug 08 18:34:58 srv borg.sh[2652992]: Keeping archive (rule: daily #4):        226cc3ae-255e-4e24-977b-db1fa32fa741 Tue, 2022-08-02 23:00:55 [271d41a84227ffa104d18073e83760e190b3ec8a255852f7f23e404b50ead583]
Aug 08 18:34:58 srv borg.sh[2652992]: Keeping archive (rule: daily[oldest] #5): 2f43aa48-f6b4-4f69-b4da-d32a3ae3bcf7 Tue, 2022-08-02 16:01:05 [e8c9e47e7dac4f502c1259fbdb19a5783b6b081203dff7d4809ef763467d4c96]
Aug 08 18:34:59 srv borg.sh[2652992]: ------------------------------------------------------------------------------
Aug 08 18:34:59 srv borg.sh[2652992]:                        Original size      Compressed size    Deduplicated size
Aug 08 18:34:59 srv borg.sh[2652992]: Deleted data:                    0 B                  0 B                  0 B
Aug 08 18:34:59 srv borg.sh[2652992]: All archives:                1.10 TB            806.57 GB             23.34 GB
Aug 08 18:34:59 srv borg.sh[2652992]:                        Unique chunks         Total chunks
Aug 08 18:34:59 srv borg.sh[2652992]: Chunk index:                  104061              5351569
Aug 08 18:34:59 srv borg.sh[2652992]: ------------------------------------------------------------------------------

相关内容