PDA

View Full Version : [SOLVED] cron + notify send



l3ecl
April 12th, 2011, 10:51 AM
I can't get cron to work out a notify send.


[jk@Ubuntu /]$ sudo crontab -l
* * * * * env DISPLAY=:0.0 /home/jk/Scripts/Alarm.sh >> /home/jk/check.log 2>&1
* * * * * DISPLAY=:0.0 /home/jk/Scripts/Alarm.sh >> /home/jk/check.log 2>&1


Cron creates an empty 'check.log' in my home directory but still doesn't display notify-send.

I've checked the 'notify-send script' manually and it works.


[jk@Ubuntu /]$ cat /home/jk/Scripts/Alarm.sh
#!/bin/bash
DISPLAY=:0.0 /usr/bin/notify-send Blink "Blink 5x" -t 7000

Rushyang
April 12th, 2011, 03:33 PM
I had the same problem before.. and then I enabled X ACL for localhost to connect to GUI applications.


~$ xhost +local:
non-network local connections being added to access control list
~$ xhost
access control enabled, only authorized clients can connect
LOCAL:
...Reference:
https://help.ubuntu.com/community/CronHowto#GUI%20Applications

Rushyang
April 12th, 2011, 03:56 PM
Also, there is no need of "DISPLAY=0.0" in your Alarm.sh, as you'll be giving it from cron.

& with notify-send "expire time" doesn't work yet. No Matter what you do, that notification will stay on your screen ~10 seconds.

PS: if you're getting work expire-time somehow.. please let me know.

DaithiF
April 12th, 2011, 06:10 PM
also, any reason why you're running this from roots crontab (sudo crontab) rather than your own user's crontab? As root you won't have the correct DBUS_SESSION_BUS_ADDRESS set that you would need to send the notification to your users gnome session, so I don't think your notify-send command will work.

l3ecl
April 13th, 2011, 04:23 AM
also, any reason why you're running this from roots crontab (sudo crontab) rather than your own user's crontab? As root you won't have the correct DBUS_SESSION_BUS_ADDRESS set that you would need to send the notification to your users gnome session, so I don't think your notify-send command will work.

i disabled the 'sudo' command and got it working perfectly, awesome. thanks!

l3ecl
April 13th, 2011, 04:26 AM
Also, there is no need of "DISPLAY=0.0" in your Alarm.sh, as you'll be giving it from cron.

& with notify-send "expire time" doesn't work yet. No Matter what you do, that notification will stay on your screen ~10 seconds.

PS: if you're getting work expire-time somehow.. please let me know.

no wonder it stayed a little too long

BkkBonanza
April 13th, 2011, 04:40 AM
It is possible to enable the timeout (-t) option but it requires replacing notify-send with a ppa package that a user rebuilt with the fix. I did that and it works. The ppa is leolik (https://launchpad.net/~leolik/+archive/leolik)

The all-knowing Ubuntu devs have decided that they don't want to support -t so other than this you'll not see it supported any time soon.

Rushyang
April 14th, 2011, 10:06 AM
also, any reason why you're running this from roots crontab (sudo crontab) rather than your own user's crontab? As root you won't have the correct DBUS_SESSION_BUS_ADDRESS set that you would need to send the notification to your users gnome session, so I don't think your notify-send command will work.

yes, you're right DBUS_SESSION_BUS_ADDRESS variable is unavailable in root environments. But, I wonder notify-send with my root cron is working perfectly... I know this because, This is because I run some cron for regular data backup & kill processes at specific time as root & to notify that task successfully completed, I use notify-send.

Rushyang
April 14th, 2011, 10:17 AM
I also wonder, is there any way to work with zenity via crons??

EDIT:
well, I just simply figured out if we add "DISPLAY=:0.0" line as prefix before calling any GTK+ dialogs (through zenity) even in scripts. They works just fine.

DaithiF
April 14th, 2011, 01:11 PM
the dbus session variable problem can be gotten around ... ie. even if you are root you can figure out the right value for a logged in user and set that value in your own environment such that notify-send appears on the user's desktop.

you pgrep for the users nautilus session, or gnome-panel, etc, ie. some process that is sure to running if that user is logged in, then from the pid grab the variable from the /proc/<pid>/environ

so something like:

pid=$(pgrep -u username nautilus)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
DBUS_SESSION_BUS_ADDRESS=$dbus

kja999
August 22nd, 2011, 11:17 AM
Just came across this thread.
Notify-send works fine with cron, just add :
export DISPLAY=:0
at the start of the script

spaceli
February 6th, 2013, 01:48 PM
Just came across this thread.
Notify-send works fine with cron, just add :
export DISPLAY=:0
at the start of the script

Marvelous!!
This works on my Ubuntu 12.04!!
Thanks!!

codemaniac
February 6th, 2013, 01:54 PM
Old thread is closed now.

As per the Ubuntu Forums Code of Conduct, please do not post in threads more than one year old.

Thanks!