这是我用于通知发送的 python 脚本:
#! /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+'"')
当我运行脚本时它正常工作,但是当尝试从 cron 运行它时它不工作
我试过从这个链接引用: Cron scheduler of python script using notify-send
即使在 crontab 中,我也尝试运行这样的通知发送命令:
* * * * * export DISPLAY=:0.0 && /usr/bin/notify-send "How are you"
但没有任何帮助。
有什么我做错的请提出建议。
最佳答案
我使用了一些不同的代码,但最终结果是您想要实现的:通过 crontab
让 notify-send
从 python 脚本工作>.
这是我的 Python 3.x 脚本:
import subprocess
subprocess.Popen(['notify-send', 'Running.'])
下面是我如何设置 crontab
:
DISPLAY=:0.0
XAUTHORITY=/home/matrix/.Xauthority
*/1 * * * * /usr/bin/python3 /home/user/path/to/my/script.py
这应该适用于 Ubuntu 16.04 和 python3,我不能代表其他操作系统,但你可以试一试。使用 which
检查您的 python
或 python3
安装路径。
当我完成“运行”的回答时。通知至少弹出 3 次。
关于python - 使用 notify-send 的 python 脚本的 crontab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36385992/