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.

2
  • What's output of running /usr/bin/dunstify hi? Make sure libnotify is installed.
    – Tuyen Pham
    Jan 7, 2020 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, 2020 at 12:06

3 Answers 3

6

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
2
  • 1
    Holy shit, that worked!! Only the XDG_RUNTIME_DIR variable seems to be necessary for notifications, though
    – aravk33
    Jan 7, 2020 at 12:53
  • I kept it the same as root's crontab , where it was necessary. Jan 7, 2020 at 15:53
0

You have to get the crontab job access to some environment variables.. I just solved this probleme in my Kali Linux, so open terminal and type export hit enter and a message with info of the variables will show up..

get to the line where declare -x DISPLAY=":1" exists (value may be 0 not 1) just copy the DISPLAY=":1" and past it in your crontab line, then go to the line where declare -x XAUTHORITY="/run/user/1000/gdm/Xauthority" exists and copy this part XAUTHORITY="/run/user/1000/gdm/Xauthority", then past that one too in your crontab line..

your crontab would be like:

* * * * * DISPLAY=':1' XAUTHORITY='/run/user/1000/gdm/Xauthority' your_command

0

In my case, the issue was with using root user with notify-send. I realized this when I saw that sudo notify-send didn't work on the terminal but just notify-send (i.e. with current user) did. So instead of editing the usual /etc/crontab which is used by root, I had a custom cron script created for myusername. Please check my whole answer here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .