问题描述
我正在使用树莓派制作Web信息亭显示板,我想在加载后2分钟将一些按键发送到浏览器窗口.该脚本发送Web服务器的登录详细信息.
我有一个脚本可以发送击键,该脚本可以从telnet控制台正常工作:
#!/usr/bash用户名=用户名"密码=密码"回显设置显示"出口DISPLAY =:0回显发送用户名"以$(sed -E s/'(.)'/'\ 1'/g<<<"$ username")中的char表示;做xdotool键$ char完毕xdotool密钥选项卡回显发送密码"对于$(sed -E s/'(.)'/'\ 1'/g<<<"$ password")中的char;做xdotool键$ char完毕xdotool键返回回声等待5秒"睡5回显设置记住密码"xdotool密钥选项卡xdotool密钥选项卡xdotool键返回回声完成"
我尝试将 bash/home/pi/logon.sh
添加到rc.local文件中-但是它不会将击键发送到浏览器吗?
有人知道为什么会这样吗?就像我说的那样-如果我运行脚本,它可以在telnet窗口中正常运行,但是从引导运行时则不起作用.
我之前在行上有 sleep 120
,如果立即触发它就停止并等待浏览器加载完毕-我知道脚本是从rc.local运行的,因为当我删除了睡眠命令,我看到了脚本的回声.
有什么想法吗?
之所以不起作用,是因为该脚本需要以pi用户身份运行.
我将rc.local脚本中的代码更改为此: su-pi -c"bash/home/pi/logon.sh&"
这使脚本以pi用户身份运行,并且&符号用于通过分叉使脚本与rc.local脚本分开运行.( http://hacktux.com/bash/ampersand )
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.
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"
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?
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.
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.
Any ideas?
The reason it wasn't working was because the script needed to be run as the user pi.
I changed the code in the rc.local script to this: 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)
这篇关于在Raspberry PI上启动后运行Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!