I can't keep a bash script alive
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?
Solution 1:
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