简体   繁体   中英

Loop function on Bash freezes my Raspberry Pi while executing xdotool commands

I have a rpi4 8gb that on the startup opens chromium and executes this Bash script

The idea is to refresh the browser every x random interval and execute some key strokes

#!/bin/bash
export XAUTHORITY=/home/sean/.Xauthority
export DISPLAY=:0

while true
do
    random=$(( $RANDOM % 1740 + 3540 )) #random number between 29 to 59 minutes
    sleep $random
    xdotool key F11  
    xdotool key "Ctrl+Shift+r" &
done

Sometimes I found that the system freezes, what could cause the problem?

My guess is that the & sign on the last line is what causes the freeze. However consider this alternative:

I recommend that instead of making an infinite loop (which could potentially freeze up your system in many ways), just assign it as a cronjob in 30min intervals.

this simplified script without loop should do it:

#!/bin/bash
export XAUTHORITY=/home/sean/.Xauthority
export DISPLAY=:0
xdotool key F11  
xdotool key "Ctrl+Shift+r"

crontab -e (choose an editor example nano)

add this line at the end of the file: (runs script every hour:00 and hour:30)

*/30 * * * * /home/sean/autorefresh_chromium.sh add the correct path

ctrl+S to save

ctrl+X to exit

That's it. It will run in the background every 30min, even after reboots. The benefit is that this way the script refreshes the browser and exits, so it is not constantly sitting on your system and it cannot get stuck.

I also use xdotool, but not on a pi. I recommend to be cautious if you are running in headless mode, because without a monitor and other GUI components xdotool might not behave as expected. Considering that you refresh a webpage I guess you have GUI so it should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM