在 Raspberry PI 上启动后运行 Shell 脚本 [英] Running Shell Script after boot on Raspberry PI

查看:52
本文介绍了在 Raspberry PI 上启动后运行 Shell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a web kiosk display board using a raspberry pi and I want to send some key strokes to the browser window 2 minutes after it's loaded. The script sends the logon details for a webserver.

我有一个脚本可以从 telnet 控制台发送可以正常工作的击键:

I've got a script that sends the keystrokes which works fine from the telnet console:

#!/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 文件 - 但它没有将击键发送到浏览器?

I've tried to add bash /home/pi/logon.sh to the rc.local file - but it doesn't send the keystrokes to the browser?

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

Does any one know why that would be? As I say - it works fine from the telnet window if I run the script, but it doesn't work when run from boot.

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

I had sleep 120 on the line before it to stop if firing right away and wait until the browser has loaded - and I know the script is running from rc.local, because when I remove the sleep command, I see the echos from the script.

有什么想法吗?

推荐答案

它不工作的原因是因为脚本需要作为用户 pi 运行.

The reason it wasn't working was because the script needed to be run as the user pi.

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

I changed the code in the rc.local script to this: su - pi -c "bash /home/pi/logon.sh &"

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

This makes the script run as the user pi and the ampersand is used to make the script run separate to the rc.local script by forking it. (http://hacktux.com/bash/ampersand)

这篇关于在 Raspberry PI 上启动后运行 Shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆