2013-05-13 18 views
15

Devo mostrare una notifica da un cron job. Il mio crontab è qualcosa di simile:Cron con notifica-invio

$ crontab -l 
# m h dom mon dow command 
    * * * * * Display=:0.0 /usr/bin/notify-send Hey "How are you" 

ho controllato /var/log/syslog e il comando venga effettivamente eseguita ogni minuto, ma non si solleva la notifica. Qualcuno può aiutarmi a capire perché?

risposta

16

ho trovato la risposta:

$ crontab -l 
# m h dom mon dow command 
    * * * * * export DISPLAY=:0.0 && export XAUTHORITY=/home/ravi/.Xauthority && sudo -u ravi /usr/bin/notify-send Hey "How are you" 

Grazie, Ravi

+3

Solo l'aggiunta di "export DISPLAY =: 0.0" ha fatto il trucco per me.Detto questo, il cronjob è in esecuzione sotto il mio account utente. XAUTHORITY è probabilmente necessario quando viene eseguito da un account diverso da quello in cui il popup deve apparire. Grazie. – Ruben

+0

Grazie, anche questo mi ha aiutato (non ho bisogno di usare "sudo") –

+2

Anch'io. Potrei ridurre questo comando a '* * * * * export DISPLAY =: 0.0 && notify-send Hey" Come stai "? – TomTom

0

ho creato uno script/usr/bin che utilizza il Display-: 0.0 tecnica http://pastebin.com/h11p2HtN

Non ci vuole XAUTHORITY in considerazione. Dovrò indagare ulteriormente.

-1

Può essere che si può provare:

* * * * * env DISPLAY=:0.0 sudo -u ravi /usr/bin/notify-send Hey "How are you"

+0

Downvote: il job cron sarà già in esecuzione come' ravi' quindi il 'sudo' non aggiungerà nulla di utile qui. – tripleee

-2

Prova questa quando si chiama notify-send nello script:

echo "PASSWORD" | sudo -u USER notify-send "your alert message"

4

In Ubuntu 14.04 esportare il display non ha funzionato per me. Di seguito è riportato uno script cron che sto utilizzando per arrestare una macchina virtuale quando lo stato della batteria di un laptop diventa troppo basso. L'impostazione della linea DBUS_SESSION_BUS_ADDRESS è la modifica che ha finalmente funzionato correttamente.

#!/bin/bash 

# if virtual machine is running, monitor power consumption 
if pgrep -x vmware-vmx; then 

    bat_path="/sys/class/power_supply/BAT0/" 

    if [ -e "$bat_path" ]; then 

    bat_status=$(cat $bat_path/status) 

    if [ "$bat_status" == "Discharging" ]; then 

     bat_current=$(cat $bat_path/capacity) 

     # halt vm if critical; notify if low 
     if [ "$bat_current" -lt 10 ]; then 

     /path/to/vm/shutdown/script 
     echo "$(date +%Y.%m.%d_%T)" >> "/home/user/Desktop/VM Halt Low Battery" 

     elif [ "$bat_current" -lt 15 ]; then 
     eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"; 
     notify-send -i "/usr/share/icons/ubuntu-mono-light/status/24/battery-caution.svg" "Virtual machine will halt when battery falls below 10% charge." 

     fi 

    fi 

    fi 

fi 

exit 0 

La linea in questione è qui:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"; 

ho trovato la soluzione qui: https://askubuntu.com/a/346580/255814

0

lavoro per me su Fedora 22:

mettere questa riga nello script .sh prima di notificare-inviare ottenere chiamato:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)" 
0

risposta semplice e ridotta:

01 * * * * export DISPLAY=:0.0 && notify-send Hey "How are you" 

Se avete bisogno di Xauthority permesso, ecco una forma generalizzabile utilizzando le variabili $LOGNAME

01 * * * * export DISPLAY=:0.0 && && export XAUTHORITY=/home/$LOGNAME/.Xauthority notify-send Hey "How are you" 

Come sottolineato da @tripleee, non c'è reale necessità di sudo qui