如何使用cron每小时在bash中启动交互式shell脚本?

ngc*_*o34 2 xorg bash cron

我把这一行放在 crontab 中:

1 * * * * DISPLAY=:0.0 /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl
Run Code Online (Sandbox Code Playgroud)

该脚本是可执行的。

如果将该命令粘贴到终端中——即没有星号的字符串,它可以完美运行:

DISPLAY=:0.0 /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl
Run Code Online (Sandbox Code Playgroud)

我试过:

sudo service cron restart
Run Code Online (Sandbox Code Playgroud)

但问题仍然存在。我怎样才能cron每小时运行该命令?


更新。让它与 ROOT crontab 一起工作:

1 * * * * DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/1000 XAUTHORITY=/home/user/.Xauthority /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 5

我想我对DISPLAY变量处理方式的第一个猜测是错误的。我认为这个问题与cron使用非交互式 shell 有某种关系。使用无限read循环的测试脚本无法运行。但是,当我通过打开新标签进行测试时:

* * * * * DISPLAY=:0 /usr/bin/gnome-terminal --tab -e /home/murukesh/test.sh
Run Code Online (Sandbox Code Playgroud)

它工作正常。


根据这一SU问题,这Ubuntu的论坛帖子,你可能不得不export$DISPLAY变量或使用env

1 * * * * env DISPLAY=:0.0 /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl
Run Code Online (Sandbox Code Playgroud)

(或者)

1 * * * * export DISPLAY=:0.0 && /usr/bin/gnome-terminal -e /home/user/Desktop/mltpl
Run Code Online (Sandbox Code Playgroud)

这可能是由于cronbesh和 not使用的 shell bash(请参阅set、export 和 env 之间有什么区别以及我应该何时使用它们?)。也看看Script 不通过 crontab 运行但独立工作正常