如何在 GUI 加载后 运行 在 Raspberry Pi 启动程序?

How do I run a program at startup on Raspberry Pi AFTER the GUI has loaded?

好的,所以我有了这个简单的 java 程序:

import java.awt.AWTException;
import java.awt.Robot;

class Program
{
    public static void main(String[] args) throws AWTException
    {
        Robot robot = new Robot();
        // do some things with the robot
    }
}

我想在 Raspberry Pi 启动时 运行 这个程序,所以我把它放在 shell 脚本中,我在 /etc/rc.local 文件。这是我的 shell 脚本:

cd /home/pi/Desktop
java Program

每当我的 Raspberry Pi 启动时,我的程序会在尝试实例化机器人 class 时抛出一个异常提示 Can't connect to X11 window server using :'0.0' as the value of the DISPLAY variable。后来我发现这是因为我的程序正在执行时GUI还没有加载,所以我在rc.local中设置了延迟。这些是 rc.local 中的最后几行:

sleep 60s
sudo sh /home/pi/Desktop/launcher.sh &
exit 0

尽管程序在 GUI 加载后 运行ning 启动,它仍然会抛出此异常。我试过:

  1. 使用 .bashrc 代替 rc.local
  2. 在调用我的实际程序之前在我的 launcher.sh 脚本中放置一个延迟
  3. 调用另一个 shell 脚本,该脚本在开始时有延迟,然后调用 launcher.sh

我没有成功,已经耗尽了我所有的想法,不知道还能在哪里寻找这个问题的解决方案。

要在 GUI 启动时启动应用程序,您应该将 .desktop 文件添加到 autostart 目录。

因此,在 ~/.config/autostart 目录中,创建 my_script.desktop 文件(用您想要的任何内容替换 my_script)。

touch my_script.desktop

对其进行编辑 (nano my_script.desktop),使其看起来像这样:

[Desktop Entry] 
Name=put_name_here
Exec=type_command_to_run_here
Type=application
Terminal=true/false (true if you want it to run in terminal)