2

I'm trying to run a bunch of scripts/commands every day.

For this purpose I've configured a user-space anacron as explained here. Anacron is started hourly by cron:

contents of /var/spool/cron/rawing:

@hourly /usr/sbin/anacron -s -t ${HOME}/.anacron/etc/anacrontab -S ${HOME}/.anacron/spool

This works fine. However, I haven't managed to run any graphical applications from my anacrontab. I've tried:

@daily 0 gui_test_env      env SCREEN=:0 zenity --info
@daily 0 gui_test_screen   SCREEN=:0 zenity --info

Both of these fail with the error message

Unable to init server: Could not connect: Connection refused

(zenity:6034): Gtk-WARNING **: cannot open display: 

Also

@daily 0 notify_test    SCREEN=:0.0 && notify-send "foobar"

didn't produce an error message, but didn't display a notification either. On the other hand, a python script that displays desktop notifications failed with the message

Unable to init server: Could not connect: Connection refused
Traceback (most recent call last):
  File "/home/rawing/Desktop/folder/python/misc/confirm_run.py", line 36, in <module>
    n.show()
GLib.Error: g-spawn-exit-error-quark: Error spawning command line 'dbus-launch --autolaunch=db6d4465f8584b8faa8abfb09b242b95 --binary-syntax --close-stderr': Child process exited with code 1 (1)

In case it matters, the python code looks like this (shortened):

from gi.repository import Notify
Notify.init('test')
n= Notify.Notification.new('title', 'question', "dialog-question")
n.set_urgency(Notify.Urgency.NORMAL)
n.show()

Adding xhost local:rawing > /dev/null to my ~/.bashrc as suggested here didn't help either.


Update: I've managed to run GUI applications using DISPLAY=:0 XAUTHORITY=/home/rawing/.Xauthority some_gui_app. Desktop notifications still don't work though. The error is:

Traceback (most recent call last):
  File "/home/rawing/Desktop/folder/python/misc/confirm_run.py", line 36, in <module>
    n.show()
GLib.Error: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files (2)

P.S.: Suggestions on how to make this work without anacron are also welcome. I thought anacron would make this easy... turns out I was too optimistic.

4
  • org.freedesktop.Notifications was not provided by any .service files I have seen reports of similar error messages (for example, here on Arch and here on Ubuntu) for which the problem was resolved by installing a notification-daemon. Have you tried that? – John1024 Mar 7 '15 at 7:37
  • @John1024: Installing notification-daemon reverted the error to GLib.Error: g-spawn-exit-error-quark: Error spawning command line 'dbus-launch --autolaunch=2a4dc02c073e450399e9f344111fbbbb --binary-syntax --close-stderr': Child process exited with code 1 (1). – Aran-Fey Mar 8 '15 at 17:22
  • That error message seems to be associated with a number of software bugs. If you are on a Mac, you might need this software update. On Fedora, you might need to upgrade to get this bug fix. Whatever distribution that you are on, you might just want to upgrade to the most recent version. – John1024 Mar 8 '15 at 20:22
  • I'm having the same issue trying to run a python script from systemd timers. (GLib.Error: g-spawn-exit-error-quark: Error spawning command line 'dbus-launch --autolaunch=</snip> -- did you figure anything out? – nak Apr 25 '15 at 3:20
1

In dealing with cron on a NAS, I've learned that executing commands directly is often counterproductive. The solution to this is to craft a script that does everything internally. Then have the cron task call the script that works perfectly. This requires a couple special actions.

  • Craft the script (this may be as simple as the extra line #!bin/bash as the first line
  • Use absolute paths for EVERYTHING (often a cron environment has no paths in the environment, if it has an environment at all)

[edit] I understand that this skips a lot of the detail provided in the question post. That is somewhat intentional.

With a bit of trial and error, here's what I got to work.

mkdir -p /home/username/bin/cron
nano -w /home/username/bin/cron/testscreen.sh

(in nano, insert the following)

#!/bin/bash
/usr/bin/env SCREEN=:0 DISPLAY=:0 XAUTHORITY=/home/username/.Xauthority /usr/bin/zenity --info

Save the script.

chmod +x /home/username/bin/cron/testscreen.sh
crontab -e 

Add the following line, adjusting the timing to your particular need.

20   8  *   *   *     /home/username/bin/cron/testscreen.sh

Mostly, I just had to look back and forth from the environment of my xterm to the environment in cron and figure out what key pieces were missing. Took some quick footwork to get cron events to trigger "a minute from now" for testing, but I got it to work, so it should work for you.

6
  • You're saying I should put "SCREEN=:0 zenity --info" in a bash script and then make cron execute that script? If so, that didn't change anything. – Aran-Fey Mar 7 '15 at 9:33
  • @Rawing Answer revised/expanded. – killermist Mar 8 '15 at 12:45
  • Like I said before, it doesn't seem to make any difference. – Aran-Fey Mar 8 '15 at 17:28
  • If you follow the steps I laid out exactly, it doesn't work? It took some work to get that set of steps down, but once I did, it works predictably every time. – killermist Mar 8 '15 at 18:30
  • GUI applications work regardless of whether I use a script, but desktop notifications don't work either way. – Aran-Fey Mar 8 '15 at 18:49

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.