首页 文章

使用notify-send的python脚本的Crontab

提问于
浏览
3

这是我发送的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

Cron with notify-send

即使在crontab中,我也尝试过运行这样的notify-send命令:

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

但没有任何帮助 .

有什么我做错了请建议 .

1 回答

  • 2

    我使用了一些不同的代码,但最终结果是你想要实现的:通过 crontab 从python脚本中获取 notify-send .

    这是我的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 检查 pythonpython3 安装的路径 .

    当我完成这个回答“跑步” . 通知至少弹出3次 .

相关问题