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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Works fine, just may want to clarify that the the home directory needs to be adapted to the user name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.