0

I'm trying to use a cron job to see when the battery gets lower than a given threshold, and then to send a battery critical notification. However, when I make the cron job execute a script every minute, and make the script send me a notification, it doesn't work.

To make sure that it's not a permissions issue with the script or something causing the cron job to not run, I made the script create a file instead, and it worked. This is the crontab entry:

* * * * * /home/aravk/test.sh

And, to simplify the problem, these are the contents of test.sh:

#!/bin/sh
/usr/bin/dunstify hi

No notification shows up, however. The script does work when I execute it manually. I also tried setting the DISPLAY environment variable to :0 by changing the crontab entry to * * * * * export DISPLAY=:0 && /home/aravk/test.sh, but it still didn't work. How do I send notifications from a script executed by a cron job? I'm on Arch Linux, if it's relevant.

  • What's output of running /usr/bin/dunstify hi? Make sure libnotify is installed. – Tuyen Pham Jan 7 at 6:25
  • @TuyenPham it shows a notification saying hi. Like I said in the question, the script does work when I execute it manually. – aravk33 Jan 7 at 12:06
2

I added this to my crontab and all my notifications work (currently tested with zenity and notify-send):

DISPLAY=":0.0"
XAUTHORITY="/home/me/.Xauthority"
XDG_RUNTIME_DIR="/run/user/1000"
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
|improve this answer|||||
  • Holy shit, that worked!! Only the XDG_RUNTIME_DIR variable seems to be necessary for notifications, though – aravk33 Jan 7 at 12:53
  • I kept it the same as root's crontab , where it was necessary. – ajgringo619 Jan 7 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.