如何显示由 crontab 触发的通知发送消息?

mem*_*und 5 bash cron notify-send 16.04

测试.sh: /usr/bin/notify-send "test"

以下工作: bash ~/Documents/test.sh

但是 crontab 从不显示通知。为什么?

*/1 * * * * bash ~/Documents/test.sh

pa4*_*080 6

要从 Cron 执行任何与 GUI 相关的应用程序,您应该导出一些桌面环境变量。以下解决方案基于此答案,其中提供了更多详细信息。

如何crontab使用 Gnome从Ubuntu 16.04、17.10、18.04 和其他 Ubuntu 发行版启动任何与 GUI 相关的应用程序

创建一个启动脚本,该脚本将导出桌面环境变量并启动您的应用程序。让我们称之为gui-launcher。它的内容应该是(引用:[1] , [2][3]):

#!/bin/bash -e

# NAME: gui-launcher

# Check whether the user is logged-in
while [ -z "$(pgrep gnome-session -n -U $UID)" ]; do sleep 3; done

# Export the current desktop session environment variables
export $(xargs -0 -a "/proc/$(pgrep gnome-session -n -U $UID)/environ")

# Execute the input command
nohup "$@" >/dev/null 2>&1 &

exit 0
Run Code Online (Sandbox Code Playgroud)
  • 对于其他桌面环境,gnome-session在这部分更改为$(pgrep gnome-session -n -U $UID)正在使用的 DE 进程的名称,例如mate-session此处提供了最多 Ubuntu DE 的列表。相同脚本的 Lubuntu 实现 -此处。该脚本可用于在当前用户的桌面会话中从 TTY 或 SSH 会话启动 GUI 应用程序。
  • 使文件可执行:chmod +x gui-launcher.
  • 该脚本将一直工作到用户登录,包括锁定屏幕。
  • 请不要以 root 身份修改和运行脚本。这可能对系统有害!

然后crontab以这种方式使用它:

*/1 * * * * /full/path/to/gui-launcher "/full/path/to/the-targer-application"
Run Code Online (Sandbox Code Playgroud)

以下是它在 Wayland 上的 Ubuntu 17.10 上的工作方式:

在此处输入图片说明



归档时间:

查看次数:

3363 次

最近记录:

6 年,8 月 前