小编MyC*_*Hat的帖子

无法让 cron 输出 GUI 消息/如何让用户知道正在执行 cron

我需要在 Ubuntu 18.04 上通过 cron 运行一个脚本,myscript.sh除其他外,我希望在运行时向用户显示一条消息,以便他知道正在运行 cron(也许还有其他方法这样做不涉及任何与 GUI 相关的事情;如果是,请告诉我)。

但是由于 cron 在它自己的最小环境中运行,我无法弄清楚如何做到这一点。

我尝试了各种方法,最有希望的似乎是明确分配 DISPLAY 并启动一个终端,分配给它的这个显示并在该终端内运行一个显示消息的命令,这意味着myscript.sH我有两行

export DISPLAY=:0 XAUTHORITY=~/.Xauthority 
gnome-terminal --display=:0.0 -- bash -c "xmessage -center -timeout 10 'ATTENTION. CRON IS RUN';exec bash"
Run Code Online (Sandbox Code Playgroud)

当我在我自己的终端中运行时,不涉及 cron,gnome-terminal --display=:0.0 -- bash -c "xmessage -center -timeout 10 'ATTENTION. CRON IS RUN';exec bash"这是有效的(当然,在我的终端中,我不需要启动另一个终端并且可以简单地运行xmessage -center -timeout 10 'ATTENTION. CRON IS RUN'......)。

但是 cron 抱怨以下消息(我将 cron 执行的输出重定向到一个文件,以便我可以看到出了什么问题):

No protocol specified
Unable to init server: Could not connect: Connection refused
Run Code Online (Sandbox Code Playgroud)

如果我不是放置 …

graphical-user-interface cron shell-scripting

0
推荐指数
1
解决办法
391
查看次数