1

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?

kurokirasama
  • 737
  • 8
  • 31
  • 2
    (1) You may need `trap '' SIGHUP` (or the like). (2) Don’t use `cron` from the 80s / 90s; there’s no reason for that in 2021. Use a `systemd` `.service` unit to keep the script alive and (re)start it on demand. Use a `system` `.timer` unit to start the script at a given time and use dependencies on `systemd` `.target` units to run the script at important system state changes. (3) When running the script manually, even when `SIGHUP` is masked, you may still need `loginctl enable-linger` for the user that runs the script. Which usually just circumvents problems. Use `systemd` units; they work. – Andrej Podzimek Oct 20 '21 at 12:59
  • Side note: This question may be a better fit for Ask Ubuntu than for Stack Overflow. – Andrej Podzimek Oct 20 '21 at 13:03
  • @AndrejPodzimek thanks for the comments, pretty advanced stuff, I'll look into it... Posting on askubuntu too. Regards, – kurokirasama Oct 20 '21 at 13:30
  • Kindly integrate this service with systemd and then run with systemctl enable/start/stop service-name accordingly. don't need to use crond here. – linux.cnf Oct 20 '21 at 16:43
  • @linux.cnf It didn't work :(... I updated the question with my results – kurokirasama Oct 20 '21 at 22:16
  • Isn’t this something you should run using `systemctl --user ...`, actually? (Apart from system-wide units, there are also per-user units.) *“Didn’t work”* sounds rather vague, considering that you have a clear error message there. So maybe start with addressing the `$DISPLAY` error message. [`Environment=` or `EnvironmentFile=`](https://kinvolk.io/docs/flatcar-container-linux/latest/setup/systemd/environment-variables/) may be the next step. Or, if it is on Wayland, figure out what needs to be configured and why the error message mentions outdated X11. – Andrej Podzimek Oct 20 '21 at 22:56
  • @AndrejPodzimek Thanks!, adding as user seem to making it work. It running for now, I'll see if it stops any time soon... – kurokirasama Oct 20 '21 at 23:21
  • @AndrejPodzimek I tried adding to systemd as user, but got issues, I updated the question with my results – kurokirasama Oct 21 '21 at 05:33
  • @kurokirasama Did you use the same unit file in `--user` mode? In `--user` mode there is no `multi-user.target`, for example. [Details are here.](https://github.com/systemd/systemd/issues/2690#issuecomment-186973730) EDIT: OK, I see, you have added an answer with this already. – Andrej Podzimek Oct 21 '21 at 11:13

1 Answers1

0

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
kurokirasama
  • 737
  • 8
  • 31