Sta*_*ann 5 command-line bash crontab cron
我想在启动时执行 bash 脚本,在不同的工作区中打开多个终端。如果我从终端调用脚本,脚本工作得很好,但如果使用 @reboot 从 crontab 执行,则脚本不起作用:
#!/usr/bin/env bash
#1 make sure we have enough workspaces
gconftool-2 --set -t int /apps/metacity/general/num_workspaces 7
#2. Launch programs in these terminals
wmctrl -s 6
gnome-terminal --full-screen --execute bash -c "tmux attach; bash"
wmctrl -s 5
gnome-terminal --full-screen --execute bash -c "weechat-curses; bash"
wmctrl -s 4
gnome-terminal --full-screen --execute bash -c "export TERM=xterm-256color; mutt; bash"
wmctrl -s 3
gnome-terminal --full-screen
wmctrl -s 2
gnome-terminal --full-screen
wmctrl -s 1
gnome-terminal --full-screen
wmctrl -s 0
google-chrome --start-maximized
Run Code Online (Sandbox Code Playgroud)
我认为这是因为 crontab 作业在加载桌面环境之前触发......也许......?加载桌面环境后如何执行此脚本?谢谢:)
更新1:
我最初是这样从 crontab 启动它的:
@reboot $HOME/andreiscripts/startup.sh >> $HOME/andreiscripts/testlog.txt 2>&1
Run Code Online (Sandbox Code Playgroud)
并收到这些错误:
Cannot open display.
Failed to parse arguments: Cannot open display:
Cannot open display.
Failed to parse arguments: Cannot open display:
Cannot open display.
.....
Run Code Online (Sandbox Code Playgroud)
更新2
我尝试从“系统”>“首选项”>“启动应用程序”启动脚本
/home/andrei/andreiscripts/startup.sh >> /home/andrei/Desktop/out.txt 2>&1
Run Code Online (Sandbox Code Playgroud)
但脚本只在工作区 6 中打开第一个 gnome 终端...并且不会继续执行脚本的其余部分,直到我关闭该 gnome 终端等等...
更新 3 - 成功
在下面寻找我的答案,看看最终起作用的脚本。
尝试这个:
sh -c /home/andrei/andreiscripts/startup.sh >> /home/andrei/Desktop/out.txt 2>&1
Run Code Online (Sandbox Code Playgroud)
那应该有效。我遇到了同样的问题,并使用 sh 运行我的脚本修复了它。
另外,您可能需要将其延迟 5 或 10 秒,以便加载其他所有内容。为此,请转到 ~/.config/autostart,找到新条目,使用文本编辑器打开它,然后添加以下行:
X-GNOME-Autostart-Delay=5
Run Code Online (Sandbox Code Playgroud)
该数字以秒为单位。使用“sleep”命令可以实现同样的效果,但是这样更干净(它不会产生新的进程)。
我也遇到过问题,尝试在启动时运行 shell 脚本,运行多个命令来生成像您这样的程序的多个实例。我能想到的唯一解决方案是在每个命令的末尾添加一个“&”。如果“sh -c”不起作用,请尝试该操作。
还有其他可能性,如果您仍然没有成功,也许我们可以尝试其他方法:)