bash 如何使用 Raspberry Pi 3 在启动时运行 Chrome?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40489508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:22:27  来源:igfitidea点击:

How can I make chromium run on startup using Raspberry Pi 3?

bashraspberry-piraspbian

提问by Adi Azarya

I have a Raspberry Pi 3 - Model B, with Raspbian jessie operation system. Im trying to open "chromium" on startup.

我有一个 Raspberry Pi 3 - B 型,带有 Raspbian jessie 操作系统。我试图在启动时打开“铬”。

i wrote a simple script:

我写了一个简单的脚本:

#!/bin/bash
/usr/bin/chromium-browser --noerordialogs --disable-session-crashed-bubble --disable-infobars --kiosk http://www.google.com
exit 0

I can run the script manually and it worksperfect. I read about a lot of various ways to run this script on startup. I have tried: adding this line @reboot path/to/my/scriptto crontab -efile with no success.
Also i have tried to edit /etc/rc.localfile by adding this line:

我可以手动运行脚本,它运行完美。我阅读了很多在启动时运行此脚本的各种方法。我试过:将此行添加@reboot path/to/my/scriptcrontab -e文件中但没有成功。
我也尝试/etc/rc.local通过添加以下行来编辑文件:

#!/bin/sh -e
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
  /home/pi/Desktop/script1.sh&   <-------- THIS LINE 
fi
exit 0

I have checked that the script is executableand rc.local too:

我已经检查过脚本是可执行的,并且 rc.local 也是:

  • rwxrwxrwx 1 pi pi script1.sh
  • rwxr-xr-x 1 root root rc.local
  • rwxrwxrwx 1 pi pi script1.sh
  • rwxr-xr-x 1 根根 rc.local

I can see script1.sh tesk on my Task Manger (it runs as root) but nothing happen.

我可以在我的任务管理器(它以 root 身份运行)上看到 script1.sh tesk,但没有任何反应。

The default user is Pi and i log as a Pi user and not as root, maybe this is the problem? Can someone explain me what is the problom and why i can see the script only in the Task Manager? what should i do ? TNX!

默认用户是 Pi,我以 Pi 用户身份登录,而不是以 root 身份登录,也许这就是问题所在?有人可以解释一下什么是问题,为什么我只能在任务管理器中看到脚本?我该怎么办 ?TNX!

UPDATEi have changed the rc.local to be like:

更新我已将 rc.local 更改为:

!/bin/sh -e
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
  su - pi -c "/usr/bin/chromium-browser --noerordialogs --disable-session-crashed-bubble --disable-infobars --kiosk http://www.google.com &"
fi
exit 0

still does not work for me :|

仍然对我不起作用:|

回答by Adi Azarya

I did a small hack...

我做了一个小黑客...

I added this line @lxterminalto the end of this file:

我将此行添加@lxterminal到此文件的末尾:

nano .config/lxsession/LXDE-pi/autostart

It will auto-start terminal on boot.

它将在启动时自动启动终端。

Then I edited $ sudo nano .bashrcfile. At the end of the file, I added my path to my script.

然后我编辑了$ sudo nano .bashrc文件。在文件的末尾,我将我的路径添加到我的脚本中。

./home/pi/Desktop/script.sh

It means that:

这意味着:

  1. The terminal will open every time you boot your Raspberry Pi (first command).

  2. Every time that terminal runs, my script will run also (second command)

  1. 每次启动 Raspberry Pi 时终端都会打开(第一个命令)。

  2. 每次终端运行时,我的脚本也会运行(第二个命令)

It does work for me. TNX for the help :)

它对我有用。TNX 的帮助:)

回答by A Brothers

Check out the verified answer on this question... Running Shell Script after boot on Raspberry PI

查看有关此问题的经过验证的答案... 在 Raspberry PI 上启动后运行 Shell 脚本

Looks like you need to run the script as the user pi.

看起来您需要以用户身份运行脚本pi

su - pi -c "/usr/bin/chromium-browser --noerordialogs --disable-session-crashed-bubble --disable-infobars --kiosk http://www.google.com &"

su - pi -c "/usr/bin/chromium-browser --noerordialogs --disable-session-crashed-bubble --disable-infobars --kiosk http://www.google.com &"

EDIT: I missed the &at the end of the command.

编辑:我错过&了命令末尾的 。

回答by adamh

Adding the shell script path directly to ~/.config/lxsession/LXDE-pi/autostart(not to ~/.bashrc) works better.

将 shell 脚本路径直接添加到~/.config/lxsession/LXDE-pi/autostart(而不是 ~/.bashrc)效果更好。

Namely it does not execute the command every terminal session (including ssh).

即它不会在每个终端会话(包括 ssh)中执行命令。

Try this in the autostart file instead:

在自动启动文件中试试这个:

@sh /home/pi/Desktop/script.sh &