I have the same issue and couldn't quickly find relevant information on the internet, so I hacked up a small Bash script.
It generates fake keyboard input, and more specifically a "left Shift", after the amount of time you specify.
This amount of time you specify, should be the amount of time it takes your computer to put the monitor to sleep/dim it. It will fake the keyboard input 5 seconds before your monitor is put to sleep/dimmed.
Run it in a terminal as follows:
Usage:
./dont_sleep.sh [-s timeinseconds]
./dont_sleep.sh [-m timeinminutes]
Values in seconds must be integers and higher than 5.
Values in minutes must be integers and at least 1.
To stop it, just press Ctrl+C or kill the process in some other way.
To make it even more usefull, use it in conjunction with
timeout; this enables you to specify how long the script should run. It's automatically terminated afterwards.
timeout 50m ./dont_sleep.sh -m 5
timeout 2h ./dont_sleep.sh -m 5
Check
man timeout.
Code:
#!/bin/bash
#
# This program takes one argument: the amount of time your computer takes before it puts the monitor to sleep (in seconds or minutes).
# It will press the left shift key for you before it happens.
# If you have dimming enabled and want to prevent that, enter the time it takes to dim the monitor.
function usage(){
echo "Usage: ./dont_sleep.sh [-s timeinseconds]"
echo " ./dont_sleep.sh [-m timeinminutes]"
echo "Values in seconds must be integers and higher than 5."
echo "Values in minutes must be integers and at least 1."
exit 0
}
if [ "$#" -ne 2 ] || ! [[ "$1" =~ -{1}. ]]; then
usage
fi
while getopts ":s:m:" opt; do
case $opt in
s) if ! [[ "$OPTARG" =~ ^[0-9]+$ ]] || [ "$OPTARG" -lt 5 ]; then
usage
fi
secs=$OPTARG
;;
m) if ! [[ "$OPTARG" =~ ^[0-9]+$ ]] || [ "$OPTARG" -lt 5 ]; then
usage
fi
mins=$OPTARG
;;
?) usage
;;
esac
done
if [ "$mins" ]; then
time=$(((60*$mins)-5))
elif [ "$secs" ]; then
time=$(($secs-5))
fi
echo "Will press left Shift every $time seconds."
while [ true ]
do
sleep $time
# Switch the comment between the next 2 lines if you prefer the mouse is moved instead.
# Check the manpage for xte for more ideas.
#xte 'mousermove 1 1'
xte 'key Shift_L'
done
PS. I'm not saying this is the best way to do it, feel free to improve.