AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1370486
Accepted
kurokirasama
kurokirasama
Asked: 2021-10-21 05:28:52 +0800 CST2021-10-21 05:28:52 +0800 CST 2021-10-21 05:28:52 +0800 CST

我无法让 bash 脚本保持活力

  • 772

我发现并稍微修改了以下脚本,该脚本监视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)

我现在缺少什么?

bash
  • 2 2 个回答
  • 134 Views

2 个回答

  • Voted
  1. Best Answer
    kurokirasama
    2021-10-21T16:45:31+08:002021-10-21T16:45:31+08:00

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

    [Unit]
    Description=notify-send log
    
    [Service]
    ExecStart=/path/to/file/notifylog
    Restart=always
    
    [Install]
    WantedBy=default.target
    
    • 1
  2. user986805
    2021-10-22T11:44:03+08:002021-10-22T11:44:03+08:00

    您可能不应该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
    
    • 0

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve