6

test.sh: /usr/bin/notify-send "test"

The following works: bash ~/Documents/test.sh

BUT the crontab never shows the notification. Why?

*/1 * * * * bash ~/Documents/test.sh

5
  • 1
    We should export some environment variables. What is your Ubuntu version and Desktop Environment?
    – pa4080
    Nov 20, 2017 at 11:59
  • 1
    @dessert, maybe $DISPLAY could be enough, but it will fail with the @reboot jobs. Recently I've found this complete solution: askubuntu.com/a/970795/566421
    – pa4080
    Nov 20, 2017 at 12:07
  • if I set DISPLAY to 99, notify-send still works in 17.10 with wayland. so $DISPLAY ist not the problem. I tested "sudo -u myuser notify-send test" and this does not work also.
    – user372194
    Nov 20, 2017 at 12:24
  • This is perhaps not only a cron problem, I can't make notify-send work from the console or over ssh.
    – pim
    Nov 20, 2017 at 12:49
  • @pa4080 Ubuntu 16.04 LTS Nov 20, 2017 at 12:53

2 Answers 2

10

To execute any GUI related app from Cron you should export few desktop environment variables. The below solution is based on this answer where are provided more details.

How to launch any GUI related application from crontab in Ubuntu 16.04, 17.10, 18.04 and other Ubuntu distributions with Gnome

Create a startup script that will export the desktop environment variables and will launch your application. Let's call it gui-launcher. Its content should be (referencs: [1], [2] and [3]):

#!/bin/bash -e

# NAME: gui-launcher

# Check whether the user is logged-in
while [ -z "$(pgrep gnome-session -n -U $UID)" ]; do sleep 3; done

# Export the current desktop session environment variables
# Sometimes it try to change $UID so we must remove -e option or use 2>/dev/null
export $(xargs -0 -a "/proc/$(pgrep gnome-session -n -U $UID)/environ") 2>/dev/null 

# Execute the input command
nohup "$@" >/dev/null 2>&1 &

exit 0
  • For other Desktop Environments change gnome-session in this part $(pgrep gnome-session -n -U $UID) with the name of the process of the DE in use, for example mate-session. A list of the most Ubuntu DE is presented here. Lubuntu implementation of the same script - here. The script could be used to launch GUI app from TTY or SSH session in the current user's Desktop session.
  • Make the file executable: chmod +x gui-launcher.
  • The script will work until the user is logged-in, including a locked screen.
  • Please don't modify and run the script as root. It could be harmful for the system!

Then use it within crontab in this way:

*/1 * * * * /full/path/to/gui-launcher "/full/path/to/the-target-application"

Here is how it works on Ubuntu 17.10 on Wayland:

enter image description here


4
  • While this may technically "work" and "solve" the primary thing asked about, it would probably be best to advise that you shouldn't do such things from cron, and if it's something that a user needs to see a notification for, should be something running under the user's session only when logged in, as running this via cron would be wasteful when logged out an the machine is on.
    – dobey
    Nov 20, 2017 at 13:19
  • @dobey, the above script will work only when the user is logged-in. But what is the actual problem when we launch GUI applications from Cron?
    – pa4080
    Nov 20, 2017 at 13:34
  • whether it is the optimal solution or not, it works! Nov 20, 2017 at 13:38
  • @pa4080 Well, "notify-send" is not a GUI application, for one, so I'm not sure what bearing that has on it. However, randomly launching GUI applications from the background, is bad design. I don't know what the specific goal is in the specific instance of this question, but there's almost certainly a better and more efficient way to achieve it, than via cron jobs.
    – dobey
    Nov 20, 2017 at 14:09
0

Environment for cron and your regular user can be different.

When you're using any scripts in cron better to put full path to them.

3
  • Using absolute path does not show the notification either Nov 20, 2017 at 12:57
  • 1
    The first sentence is the actual issue. The second sentence is not a problem. DBUS_SESSION_BUS_ADDRESS is not set in the cron environment, and so notify-send can't talk to the notification service, and dbus will try to start a new session bus and fail.
    – dobey
    Nov 20, 2017 at 13:16
  • @dobey is right. Exporting DBUS_SESSION_BUS_ADDRESS solved my problem. Apr 21, 2020 at 8:52

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