AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 25556
Accepted
Stann
Stann
Asked: 2011-02-10 16:42:27 +0800 CST2011-02-10 16:42:27 +0800 CST 2011-02-10 16:42:27 +0800 CST

加载桌面后执行脚本?

  • 772

我想在启动时执行 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

我认为这是因为 crontab 作业在加载桌面环境之前触发......也许......?加载桌面环境后如何执行此脚本?谢谢:)

更新1:

我最初是从 crontab 开始的,如下所示:

@reboot $HOME/andreiscripts/startup.sh >> $HOME/andreiscripts/testlog.txt 2>&1

并收到这些错误:

Cannot open display.
Failed to parse arguments: Cannot open display: 
Cannot open display.
Failed to parse arguments: Cannot open display: 
Cannot open display.
.....

更新 2

我尝试从 System > Preferences > Startup Applications 启动脚本

/home/andrei/andreiscripts/startup.sh >> /home/andrei/Desktop/out.txt 2>&1

但是脚本只在工作区 6 中打开了第一个 gnome-terminal ...并且在我关闭该 gnome-terminal 之前不会继续执行脚本的其余部分等等....

更新 3 - 成功

在下面查找我的答案,以查看最终有效的脚本。

bash
  • 4 4 个回答
  • 13076 Views

4 个回答

  • Voted
  1. ayan4m1
    2011-02-10T16:47:37+08:002011-02-10T16:47:37+08:00

    尝试系统 > 首选项 > 启动应用程序。您应该能够为您的 bash 脚本添加一个条目。这些应用程序作为登录后桌面加载过程的一部分启动。

    • 4
  2. Best Answer
    Deadite81
    2011-02-12T06:40:43+08:002011-02-12T06:40:43+08:00

    尝试这个:

    sh -c /home/andrei/andreiscripts/startup.sh >> /home/andrei/Desktop/out.txt 2>&1
    

    那应该行得通。我遇到了同样的问题并用 sh 运行我的脚本修复了它。

    此外,您可能希望将其延迟 5 或 10 秒,以便加载其他所有内容。为此,请转到 ~/.config/autostart,找到您的新条目,使用文本编辑器打开它,然后添加以下行:

    X-GNOME-Autostart-Delay=5
    

    数字以秒为单位。使用“sleep”cmd 可以实现相同的目的,但这更清洁(它不会产生新进程)。

    我也遇到了问题,试图在启动时运行 shell 脚本,运行多个命令,这些命令会产生像你这样的程序的多个实例。我能想出的唯一解决方案是在每个命令的末尾添加一个“&”。如果“sh -c”不起作用,请尝试。

    还有其他的可能性,如果你还没有让它工作,也许我们可以尝试其他的:)

    • 4
  3. Erik Johansson
    2011-02-12T08:38:43+08:002011-02-12T08:38:43+08:00

    创建 6 个 .desktop 启动器来启动这些命令,然后保存会话。

    • 1
  4. Stann
    2011-02-12T16:07:01+08:002011-02-12T16:07:01+08:00

    这是有效的脚本:

    #!/usr/bin/env bash
    
    #1 make sure we have enough workspaces
    wmctl -n 7
    
    #2. Launch programs in these terminals
    gnome-terminal --title=tmux --full-screen --execute bash -c "tmux attach; bash" &
    sleep 0.3
    wmctrl -r :ACTIVE: -N "tmux"
    gnome-terminal --full-screen --execute bash -c "weechat-curses; bash" &
    sleep 0.3
    wmctrl -r :ACTIVE: -N "weechat"
    gnome-terminal --full-screen --execute bash -c "export TERM=xterm-256color; mutt; bash" &
    sleep 0.3
    wmctrl -r :ACTIVE: -N "mutt"
    gnome-terminal --full-screen &
    sleep 0.3
    wmctrl -r :ACTIVE: -N "bash1"
    gnome-terminal --full-screen &
    sleep 0.3
    wmctrl -r :ACTIVE: -N "bash2"
    gnome-terminal --full-screen --execute bash -c "export TERM=xterm-256color;vim;bash" &
    sleep 0.3
    wmctrl -r :ACTIVE: -N "vim"
    google-chrome --start-maximized &
    
    sleep 2
    
    wmctrl -r tmux -t 6
    wmctrl -r weechat -t 5
    wmctrl -r mutt -t 4
    wmctrl -r bash2 -t 3
    wmctrl -r bash1 -t 2
    wmctrl -r vim -t 1
    wmctrl -r Chrome -t 0
    

    我按照 ayan4m1 的建议从 System > Preferences > Startup Applications 启动它,如下所示:

    /home/andrei/scripts/startup.sh >> /home/andrei/scripts/out.txt 2>&1
    
    • 0

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    我需要什么命令来解压缩/提取 .tar.gz 文件?

    • 8 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Martin Hope
    EmmyS 我需要什么命令来解压缩/提取 .tar.gz 文件? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve