crontab -e
然后我确实添加了*/2 * * * * /usr/bin/notify-send "test"
,它应该每 2 分钟发送一次通知,但它不起作用。
crontab 文件如下所示。
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/2 * * * * /usr/bin/notify-send "test"
答案1
我认为 Cron 是作为系统守护进程运行的,并且不知道您的桌面环境会话,尽管它需要显示通知。您可以编写脚本,发送通知并休眠 2 分钟,然后在后台以用户身份运行它。此外,如果您想要的正是 Cron,您可以创建脚本,该脚本首先导出桌面标识符export DISPLAY=:0
,然后notify-send
从 Cron 调用并调用此脚本,或者只需将您的 Cron 行替换为*/2 * * * * DISPLAY=:0 /usr/bin/notify-send "test"
。
注意 - 对我来说它起作用乌buntu 16.04 但不在钾ubuntu 19.10。尚未在其他系统版本上测试过。