1

我发现并稍微修改了以下脚本,该脚本监视notify-send通知并将它们转储到文件中。

#!/bin/bash

logfile=$1

dbus-monitor "interface='org.freedesktop.Notifications'" |\
 grep --line-buffered "string" |\
 grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v |\
 grep --line-buffered '.*(?=string)|(?<=string).*' -oPi |\
 grep --line-buffered -v '^\s*$' |\
 ts |\
 xargs -I '{}' -d '\n' echo -e {} >> $logfile

如果我手动运行它:

notifylog notifylog.txt

该过程会持续工作一段时间,但最终会停止。如果我将它添加到 crontab 中,例如:

@reboot /path/to/file/notifylog /home/user/notifylog.txt

它执行一次然后停止(或者它最后一次运行很少)。

我什至尝试将其添加到启动应用程序中,例如:

/path/to/file/notifylog /home/user/notifylog.txt

和相同的结果。以下内容在手动执行但不能从 crontab 或启动应用程序执行时有效:

#!/bin/bash

logfile='/home/user/notifylog.txt'
rm -f $logfile
touch $logfile

while true; do /path/to/file/notifylog $logfile && break;done

我通过以下步骤添加到 systemd:

须藤纳米/lib/systemd/system/notifylog.service

然后我补充说:

[Unit]
Description=notify-send log

[Service]
ExecStart=/path/to/file/notifylog

[Install]
WantedBy=multi-user.target

然后:

sudo systemctl daemon-reload
sudo systemctl enable notifylog.service
sudo systemctl start notifylog.service
sudo systemctl status notifylog.service

最后一个给了我:

● notifylog.service - notify-send log
     Loaded: loaded (/lib/systemd/system/notifylog.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Wed 2021-10-20 19:01:49 -03; 3min 52s ago
    Process: 364180 ExecStart=/path/to/file/notifylog (code=exited, status=0/SUCC>
   Main PID: 364180 (code=exited, status=0/SUCCESS)

oct 20 19:01:49 mymachine systemd[1]: Started notify-send log.
oct 20 19:01:49 mymachine notifylog[364186]: Failed to open connection to session bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
oct 20 19:01:49 mymachine systemd[1]: notifylog.service: Succeeded.

它似乎没有运行。

为此,我稍微修改了脚本:

#!/bin/bash

logfile='/home/user/notifylog.txt'
rm -f $logfile
touch $logfile

dbus-monitor "interface='org.freedesktop.Notifications'" |\
 grep --line-buffered "string" |\
 grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v |\
 grep --line-buffered '.*(?=string)|(?<=string).*' -oPi |\
 grep --line-buffered -v '^\s*$' |\
 ts |\
 xargs -I '{}' -d '\n' echo -e {} >> $logfile

编辑:现在我通过以下步骤将它作为用户添加到 systemd

首先,将 .service 文件添加到/home/user/.config/systemd/user. 然后执行:

sudo systemctl daemon-reload
systemctl --user enable notifylog.service
systemctl --user start notifylog.service
systemctl --user status notifylog.service

这会正确启动服务,但是如果我重新启动机器,

systemctl --user status notifylog.service

给我:

● notifylog.service - notify-send log
     Loaded: loaded (/home/user/.config/systemd/user/notifylog.service; enabled; vendor preset: enabled)
     Active: inactive (dead)

我现在缺少什么?

4

1 回答 1

0

到目前为止起作用的是更改该WantedBy部分:

[Unit]
Description=notify-send log

[Service]
ExecStart=/path/to/file/notifylog
Restart=always

[Install]
WantedBy=default.target
于 2021-10-21T00:45:56.863 回答