How to run a bash script from crontab and send the output to desktop notifications

I'm trying to get the RAID status every time i boot into my system (Debian) and send the output as notification in the desktop. This works as expected from terminal: sudo mdadm --detail /dev/md0 | grep 'Working Devices :' | while read OUTPUT; do notify-send "$OUTPUT"; done

but it won't work if I run a crontab job pointing at the script :

mdadm --detail /dev/md0 | grep 'Working Devices :' | while read OUTPUT; do notify-send "$OUTPUT"; done

The script is set to be executable and is ran from crontab root as:

@reboot /PATH/scripth.sh

this also won't work:

* * * * * /PATH/scripth.sh

and logs are not produced when:

* * * * * /PATH/scripth.sh > /PATH/crontab.log

1 answer

  • answered 2019-10-09 12:21 Ĺ erg

    This work in my PC (change username):

    sudo mdadm --detail /dev/md0 | grep 'Working Devices :' | while read OUTPUT; do su - RealUserName -c "DISPLAY=':0.0' notify-send \"$OUTPUT\""; done