我找到并稍微修改了以下腳本,該腳本監視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
This start the service correctly, but if I reboot my machine,
systemctl --user status notifylog.service
gives me:
● notifylog.service - notify-send log
Loaded: loaded (/home/user/.config/systemd/user/notifylog.service; enabled; vendor preset: enabled)
Active: inactive (dead)
What I'm missing now?
uj5u.com熱心網友回復:
到目前為止有效的是更改該WantedBy部分:
[Unit]
Description=notify-send log
[Service]
ExecStart=/path/to/file/notifylog
Restart=always
[Install]
WantedBy=default.target
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/333635.html
