Skip to main content
Topic: Notification from Cronjobs not working (Read 2413 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Notification from Cronjobs not working

This https://wiki.archlinux.org/title/Cron#Running_X.org_server-based_applications  says that,
to have notifications from a cronjob,  you have to specify `DBUS_SESSION_BUS_ADDRESS` and `DISPLAY` variables.
The `DISPLAY` variable is easy, it is allways ':0'
But the problem is `DBUS_SESSION_BUS_ADDRESS`. Doing this in a terminal:
Code: [Select]
$ echo $DBUS_SESSION_BUS_ADDRESS
unix:abstract=/tmp/dbus-TmOWK21wNR,guid=1770ea5d0622fa8ebc76a544612cc0ad
the variable is different every login, so i cant specify it in the crontab.

Now, https://wiki.archlinux.org/title/Cron#Running_X.org_server-based_applications says that dbus var should be like this:
Code: [Select]
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u $USER)/bus
but in Artix it will be this:
Code: [Select]
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u $USER)/bus-1
but this doesnt work, because it is not true, because like i said doing this in a terminal:
Code: [Select]
$ echo $DBUS_SESSION_BUS_ADDRESS
unix:abstract=/tmp/dbus-TmOWK21wNR,guid=1770ea5d0622fa8ebc76a544612cc0ad
tells you the real value.
so to have it always the same, i wrote this in my zprofile:
Code: [Select]
export DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/1000/bus-1'
But, that breaks dunst = doesn't run

# Temporary fix:
writing this in my `zprofie`:
Code: [Select]
export DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-TmOWK21wNR,guid=1770ea5d0622fa8ebc76a544612cc0ad"
and then having this in my crontab
Code: [Select]
* * * * * export DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-TmOWK21wNR,guid=1770ea5d0622fa8ebc76a544612cc0ad"; export DISPLAY=':0'; notify-send "beep bop"
...then it works, BUT like i said earlier, every login the dbus variable will change, so the crontab will not have the real vaule.
and just donig only this:
Code: [Select]
* * * * * export DISPLAY=':0'; notify-send "beep bop"
does not work.

Re: Notification from Cronjobs not working

Reply #1
What X program do you need to run with cron?

Conceptually, cron is meant for programs which don't interact with the user and are highly automated (for example, mail check). X programs don't fit that purpose.

If you want to notify the user from a cronjob, you can do something like:
Code: [Select]
        /usr/bin/env DISPLAY=:0 /usr/bin/notify-send -i $ICON "$TITLE" "$MESSAGE"
This is a line from my mail checking script, which I have in my crontab.

Re: Notification from Cronjobs not working

Reply #2
accidentaly i pressed post instead of preview, sorry.
@strajder  please read my OriginalPost again, cause i have allot of other info.

Re: Notification from Cronjobs not working

Reply #3
additional info
first my zprofile runs, then my xprofile, and then my xinitrc (well first xinit but first it sources xprofile before running itself)
XINITRC:
Code: [Select]
# export STATUSBAR="i3blocks" # Uncomment this line when using i3.

if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/x11/xprofile" ]; then
. "${XDG_CONFIG_HOME:-$HOME/.config}/x11/xprofile"
else
. "$HOME/.xprofile"
fi

# ssh-agent dwm
# ssh-agent i3
eval $(dbus-launch --sh-syntax --exit-with-session)
dbus-launch --exit-with-session dwm

XPROFILE:
Code: [Select]
#!/bin/sh

# Fix Gnome Apps Slow  Start due to failing services
# Add this when you include flatpak in your system
# dbus-update-activation-environment --systemd DBUS_SESSION_BUS_ADDRESS DISPLAY XAUTHORITY

#xrdb ${XDG_CONFIG_HOME:-$HOME/.config}/x11/xresources & # Uncomment to use Xresources colors/settings on startup
lxpolkit & # ask you for root pw
remaps & # run the remaps script, switching caps/esc and more; check it for more info
xcompmgr & # xcompmgr for transparency
dunst & # dunst for notifications
xset r rate 300 50 & # Speed xrate up
unclutter & # Remove mouse when idle
{ mpd && sb-music ;} & # music player
{ fuego-only ; setbg ;} & # resolution and background
light -N 10 & light -S 100 &
redshift -l 42.66:21.16 -t 6500:3000 >/dev/null 2>&1 &
mpv --no-audio-display --volume=50 "$XDG_DATA_HOME"/win_XP_Startup.mp3 >/dev/null 2>&1 &
/usr/lib/kdeconnectd & #KDE-Connect app for phones
#xset b 50 21 120 #nice beep

# This line autostarts an instance of Pulseaudio that does not exit on idle.
# This is "necessary" on Artix due to a current bug between PA and
# Chromium-based browsers where they fail to start PA and use dummy output.
pidof -s runit &&
! pidof -s pulseaudio >/dev/null 2>&1 &&
setsid -f pulseaudio --start --exit-idle-time=-1 >/dev/null 2>&1
# vim: ts=8 sw=8

Re: Notification from Cronjobs not working

Reply #4
From man 5 crontab:
Quote
       An active line in a crontab is either an environment setting or a cron command.  An environment setting is of the form:
[...]
       The format of a cron command is similar to the V7 standard, with a number of upward-compatible extensions.  Each line has five time-and-date fields followed by a username (if this
       is  the system crontab file), and followed by a command.  Commands are executed by cron(8) when the 'minute', 'hour', and 'month of the year' fields match the current time, and at
       least one of the two 'day' fields ('day of month', or 'day of week') match the current time (see "Note" below).
Note "a command", not "multiple commands".

Next, it is best to use /usr/bin/env instead of export, in order not to pollute the environment of subsequent commands.

Also see: https://askubuntu.com/a/205698

Re: Notification from Cronjobs not working

Reply #5
@strajder
Quote
Note "a command", not "multiple commands".
but i am using only one command (notify-send "beep bop"), what do you mean?

Quote
Next, it is best to use /usr/bin/env instead of export, in order not to pollute the environment of subsequent commands.
so should my crontab look like this:
Code: [Select]
* * * * * /usr/bin/env DISPLAY=:0 /usr/bin/notify-send "beep bop"
because i have tried that and it did not work.

Re: Notification from Cronjobs not working

Reply #6
Code: [Select]
* * * * * /usr/bin/env DISPLAY=:0 /usr/bin/notify-send "beep bop"
because i have tried that and it did not work.
This command works for me. It sends a notification every minute.

Can you post your /var/log/crond.log (use paste.artixlinux.org)?

Re: Notification from Cronjobs not working

Reply #7
Quote
This command works for me. It sends a notification every minute.
Really? it has never worked for me. Even in arch, i always had to write it with the dbus env var also.

here is the log:  https://paste.artixlinux.org/view/raw/c60a9015

Re: Notification from Cronjobs not working

Reply #8
Do you have an X session running when testing? This requires a running X session (on :0).

What happens when you do
Code: [Select]
$ notify-send Test
from a terminal emulator under X?

Edit: Also make sure you have both dbus and dbus-<your_init> installed and the dbus service setup and running for your init. I use 66, so I have dbus-suite66 installed. You should also probably ditch that dbus-launch line.

Edit 2: This is my .xinitrc. I use dwm. It needs to end with an exec line.

Re: Notification from Cronjobs not working

Reply #9
yes of course, i am using dwm

i installed artix lxde runit

so it has cronie installed and enabled by default


Re: Notification from Cronjobs not working

Reply #11
@strajder  sorry but i dont understand you. i said that i have it installed, and enabled.

but anyhow, i re-did it
Code: [Select]
sudo pacman -S dbus-runit
sudo ln -sf /etc/runit/sv/cronie /run/runit/service/
sudo sv restart cronie

still not successful.


Re: Notification from Cronjobs not working

Reply #13
@strajder bruh which?
the x session one?
well yes, i am running dwm, so yes when i run, in my terminal `notify-send Test` it works. i have mentioned this in my OP.

and if you mean, your xinit, which i just now saw, yes i know that. but i have been told, in the artix wiki and artix telegram group that  i should use `dbus-launch --exit-with-session dwm` instead of `exec dwm`.
doesnt make a diff though anyhow.

Re: Notification from Cronjobs not working

Reply #14
and if you mean, your xinit, which i just now saw, yes i know that. but i have been told, in the artix wiki and artix telegram group that  i should use `dbus-launch --exit-with-session dwm` instead of `exec dwm`.
Link?

https://wiki.archlinux.org/title/Xinit#xinitrc
Quote
Long-running programs started before the window manager, such as a screensaver and wallpaper application, must either fork themselves or be run in the background by appending an & sign. Otherwise, the script would halt and wait for each program to exit before executing the window manager or desktop environment. Note that some programs should instead not be forked, to avoid race bugs, as is the case of xrdb. Prepending exec will replace the script process with the window manager process, so that X does not exit even if this process forks to the background.

https://dwm.suckless.org/tutorial/
Quote
Launching
To launch dwm, ideally you should setup a ~/.xinitrc with at least exec dwm.