【发布时间】:2021-12-07 07:00:26
【问题描述】:
我找到并稍微修改了以下脚本,该脚本监控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) 您可能需要
trap '' SIGHUP(或类似名称)。 (2)不要使用80/90后的cron;在 2021 年没有理由这样做。使用systemd.service单元来保持脚本处于活动状态并按需(重新)启动它。使用system.timer单元在给定时间启动脚本,并使用对systemd.target单元的依赖关系在重要的系统状态更改时运行脚本。 (3) 手动运行脚本时,即使SIGHUP被屏蔽,运行脚本的用户可能仍需要loginctl enable-linger。这通常只是规避问题。使用systemd单位;他们工作。 -
旁注:这个问题可能比 Stack Overflow 更适合 Ask Ubuntu。
-
@AndrejPodzimek 感谢 cmets,非常先进的东西,我会研究它......也在 askubuntu 上发帖。问候,
-
请将此服务与 systemd 集成,然后相应地使用 systemctl enable/start/stop service-name 运行。这里不需要使用 crond。
-
@linux.cnf 没用 :(...我用我的结果更新了问题
标签: bash cron startup notify-send