This is my attempt to make a script that will use a "mouse move" to prevent screen blanking while watching videos. I'm hoping to use the value for highest CPU% process in top
and if CPU usage exceeds 5%, a mouse move should occur.
#!/usr/bin/env bash
sleep_period=60s
while true; do
if (( $(top -bn 1 | sed -nrs '8p' | awk '{ print $9 }') -gt 5 )); then
while (( $(top -bn 1 | sed -nrs '8p' | awk '{ print $9 }') -gt 5 )); do
xdotool mousemove 0 100
xdotool mousemove 0 50
sleep ${sleep_period}
done
else
sleep ${sleep_period}
fi
done
Unfortunately, it does not work. The errors are like this:
[07:20 PM] /bin $ noo.sh
/home/vasa1/bin/noo.sh: line 6: ((: 0.0 -gt 5 : syntax error: invalid arithmetic operator (error token is ".0 -gt 5 ")
/home/vasa1/bin/noo.sh: line 6: ((: 6.4 -gt 5 : syntax error: invalid arithmetic operator (error token is ".4 -gt 5 ")
How do I fix this? (Please note that I'm not experienced in scripting.)
Based on answers here, I put together:
#!/usr/bin/env bash
sleep_period=5m
while true; do
if [[ $(top -bn 1 | sed -nrs '8p' | awk '{ print int($9) }') -gt 8 ]]; then
while [[ $(top -bn 1 | sed -nrs '8p' | awk '{ print int($9) }') -gt 8 ]]; do
xset -dpms; xset s off
xset +dpms; xset s on
sleep ${sleep_period}
done
else
sleep ${sleep_period}
fi
done
Then, I reported this code over at Ubuntu Forums and Vaphell worked on it further. Below is Vaphell's version and is what I am using:
#!/usr/bin/env bash
sleep_period=5m
while true; do
if top -bn 1 | awk 'NR==8 { exit !($9>8); }'; then
xset -dpms; xset s off
xset +dpms; xset s on
fi
sleep ${sleep_period}
done
-gt
expects integer operands, not floating point. and i'm not sure why you're using((
...))
rather than just[
...]
. or why you're using CPU% as the trigger...or even why you're extracting CPU% utilisation from a curses program like top rather than something likeps -heo %C --sort -%cpu | head -1
– cas Sep 18 '13 at 14:33gnome-sceensaver
and maybe others. I haven't looked into it for a while but it used to drive me up the wall too. See here for example. – terdon Sep 18 '13 at 14:37xset
as suggested by Raphael below. – cas Sep 18 '13 at 15:02