linux - 在 Raspberry PI 上启动后运行 Shell 脚本

标签 linux bash shell raspberry-pi raspbian

我正在使用 raspberry pi 制作一个网络信息亭显示板,我想在加载 2 分钟后将一些击键发送到浏览器窗口。该脚本发送网络服务器的登录详细信息。

我有一个脚本可以发送在 telnet 控制台上运行良好的击键:

#!/usr/bash
username="username"
password="password"
echo "Setting Display"
export DISPLAY=:0
echo "Sending Username"
for char in $(sed -E s/'(.)'/'\1 '/g <<<"$username"); do
    xdotool key $char
done
xdotool key Tab
echo "Sending Password"
for char in $(sed -E s/'(.)'/'\1 '/g <<<"$password"); do
    xdotool key $char
done
xdotool key Return
echo "Waiting 5 Seconds"
sleep 5
echo "Setting Remember Password"
xdotool key Tab
xdotool key Tab
xdotool key Return
echo "Finished"

我已经尝试将 bash/home/pi/logon.sh 添加到 rc.local 文件 - 但它不会将击键发送到浏览器?

有人知道为什么会这样吗?正如我所说 - 如果我运行脚本,它在 telnet 窗口中工作正常,但在从引导运行时它不起作用。

我在它之前的行上有 sleep 120 如果立即触发就停止并等待浏览器加载 - 我知道脚本正在从 rc.local 运行,因为当我删除 sleep 命令,我看到了脚本的 echo 。

有什么想法吗?

最佳答案

它不起作用的原因是脚本需要以用户 pi 的身份运行。

我将 rc.local 脚本中的代码更改为:su - pi -c "bash/home/pi/logon.sh &"

这使得脚本以用户 pi 的身份运行,并且符号用于通过 fork 使脚本独立于 rc.local 脚本运行。 ( http://hacktux.com/bash/ampersand )

关于linux - 在 Raspberry PI 上启动后运行 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21578852/

相关文章:

linux - 如果我使用 makefile,如何在 .c 文件中包含 .h 文件

bash - 从 Docker 中的 bash 进行日志记录 : logger error: socket/dev/log: No such file or directory

linux - 使用与主机上不同的用户启动 Docker 容器

bash - 使用 `read` 读取密码时如何回显星号 (*)?

linux - "./somescript.sh"和 ". ./somescript.sh"有什么区别

linux - 使用 Groovy 和 SED 命令用变量替换字符串

linux - 如何从 UNIX 中的文件中删除单词?

java - 如何在 Linux 中启动时自动启动基于 Gradle 的 Java 应用程序/守护进程?

linux - BASH 脚本中的日历月变量

macos - 带有数字名称的 bash for 循环