我无法让 Bash 脚本存活

我无法让 Bash 脚本存活

我找到并稍微修改了以下脚本,该脚本监视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:

sudo nano /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)

我现在缺少什么?

答案1

到目前为止有效的是改变该WantedBy部分:

[Unit]
Description=notify-send log

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

[Install]
WantedBy=default.target

答案2

您可能不应该dbus-monitor在文本模式下解析,最好使用JSON

#!/bin/bash

coproc P {
    exec busctl --user --json=short \
    --match="interface=org.freedesktop.Notifications,member=Notify" monitor
}

format='%s
App: %s\nIcon: %s\nSummary: %s\nBody: %s\n'

while read -ru ${P[0]}; do
    mapfile -t < <( \
        jq '.payload.data[0,2,3,4]' <<< "$REPLY" \
    )
    printf "$format" "-- Notification --" "${MAPFILE[@]}" | ts
done

相关内容