Ravinder Baid
Ravinder Baid

Reputation: 395

Crontab of python script using notify-send

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.

Upvotes: 4

Views: 1456

Answers (1)

imrek
imrek

Reputation: 3040

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.

Upvotes: 5

Related Questions