4

This is my python script for notify send:

#! /usr/bin/env python
import os
mstr='The scoreis 102/3'
title="SCORE"
os.environ.setdefault('DISPLAY', ':0.0')
os.system('notify-send -i "notification-message-IM" "'+title+'" "'+mstr+'"')

It works normally when I run the script,but while trying to run it from cron it is not working

I have tried reference from this links: Cron scheduler of python script using notify-send

Cron with notify-send

Even in crontab i have tried to run a notify-send command like this:

* *  *   *   * export DISPLAY=:0.0 && /usr/bin/notify-send "How are you"

But of no help.

Is there anything I am doing wrong please suggest.

Community
  • 1
  • 1
Ravinder Baid
  • 395
  • 1
  • 15

1 Answers1

4

I'm using a little bit different code, but the end result is what you were trying to achieve: get notify-send to work from a python script via crontab.

Here's my Python 3.x script:

import subprocess
subprocess.Popen(['notify-send', 'Running.'])

And here's how I setup crontab:

DISPLAY=:0.0
XAUTHORITY=/home/matrix/.Xauthority
*/1 * * * * /usr/bin/python3 /home/user/path/to/my/script.py

This should work with Ubuntu 16.04 and python3, I cannot speak for other OS's, but you can give it a try. Use which to check the path of your python or python3 installation.

By the time I finished this answer the "Running." notification popped up at least 3 times.

imrek
  • 2,930
  • 3
  • 20
  • 36
  • Works fine, just may want to clarify that the the home directory needs to be adapted to the user name. – Stefan Oct 26 '17 at 13:27