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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21578852/
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 09:28:19  来源:igfitidea点击:

Running Shell Script after boot on Raspberry PI

linuxbashshellraspberry-piraspbian

提问by Blind Trevor

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.

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

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

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

#!/usr/bash
username="username"
password="password"
echo "Setting Display"
export DISPLAY=:0
echo "Sending Username"
for char in $(sed -E s/'(.)'/' '/g <<<"$username"); do
    xdotool key $char
done
xdotool key Tab
echo "Sending Password"
for char in $(sed -E s/'(.)'/' '/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"

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

我试图添加bash /home/pi/logon.sh到 rc.local 文件 - 但它没有将击键发送到浏览器?

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.

有谁知道为什么会这样?正如我所说 - 如果我运行脚本,它可以从 telnet 窗口正常工作,但从启动运行时它不起作用。

I had sleep 120on 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.

sleep 120上线之前就停止射击,如果马上,等待浏览器加载-我知道剧本是从rc.local的运行,因为当我删除sleep命令,我看到从脚本上的回波。

Any ideas?

有任何想法吗?

回答by Blind Trevor

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

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

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

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

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)

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

回答by ihatetoregister

Put this in your crontab

把它放在你的 crontab 中

@reboot /path/to/script

Edit it using

使用编辑它

#crontab -e