Cron with notify-send

前端 未结 13 1217
我寻月下人不归
我寻月下人不归 2020-12-09 10:06

I need to show a notification from a cron job. My crontab is something like:

$ crontab -l
# m h  dom mon dow   command
  * *   *   *   *    Display=:0.0 /usr         


        
相关标签:
13条回答
  • 2020-12-09 10:23

    If you are having issue that your notify-send script is working fine locally but it is not working with cron jobs then use this command. and replace "saurav" with your username.

    sudo -u saurav DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Your message goes here"
    
    0 讨论(0)
  • 2020-12-09 10:25

    On recent Ubuntu versions, the following should work.

    #notify_me.sh, can be placed e.g. in your home directory
    
    #!/bin/bash
    eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
    # the actual notification
    DISPLAY=:0 notify-send "Notify me!"
    

    Then you add a line to your user's cronjobs via crontab as usual.

    0 讨论(0)
  • 2020-12-09 10:25

    Similar to nmax above, I also solved the issue by setting DBUS_SESSION_BUS_ADDRESS environment variable. However, I am using a combination of Linux Mint 19 (xfce4) and XMonad and for some reason, I don't have the process xfce4-session running. Instead, I found that xfce4-terminal is (usually) running, which resulted in the following line in the beginning of my script:

    eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-terminal)/environ)"
    

    This resolved the issue for me.

    0 讨论(0)
  • 2020-12-09 10:28

    Simple and reduced answer:

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

    If you need Xauthority permission, here's a generalizable form using the $LOGNAME variable

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

    As pointed out by @tripleee, there's no real need for sudo here

    0 讨论(0)
  • 2020-12-09 10:29

    Try this when you call notify-send in your script:

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

    0 讨论(0)
  • 2020-12-09 10:30

    Work for me on fedora 22:

    Put this line in the .sh script before notify-send get called:

    eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"
    
    0 讨论(0)
提交回复
热议问题