Score:0

I can't keep a bash script alive

us flag

I found and slightly modify the following script, that monitor the notify-send notifications and dump them in a file.

#!/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

If I run it manually:

notifylog notifylog.txt

the process keeps working for a while but eventually stops. If I add it to crontab like:

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

it executes once and then stops (or it last running very little).

I even tried adding it to the startup applications like:

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

and same result. The following works when executed manually but not from crontab or startup applications:

#!/bin/bash

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

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

I added to systemd with the following steps:

sudo nano /lib/systemd/system/notifylog.service

then I added:

[Unit]
Description=notify-send log

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

[Install]
WantedBy=multi-user.target

then:

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

the last one gives me:

● 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.

It doesn't seems to be running.

For this I modified the script a little:

#!/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

EDIT: now I added it to systemd as user with the following steps

First, add the .service file to /home/user/.config/systemd/user. Then execute:

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?

bac0n avatar
cn flag
You get the same problem with systemd?
kurokirasama avatar
us flag
I'm looking into how to do it with systemd... first time trying
kurokirasama avatar
us flag
@bacon it didn't work :(
kurokirasama avatar
us flag
@bac0n I updated the question with my results
Score:1
us flag

What worked so far was changing the WantedBy section:

[Unit]
Description=notify-send log

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

[Install]
WantedBy=default.target
Score:0
cn flag

You should probably not parse dbus-monitor in text mode, better using 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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.