Bash Script To Keep Mouse Moving On Ubuntu

In order to prevent my computer from going to sleep when running processes that take a long time I sometimes run a simple mouse move script. This will randomly move my mouse around the screen and keep the computer from sleeping.

To use this script you'll need to install the xdotool, which can be done on Ubuntu using the apt command.

apt install xdotool

Just copy the following script and pop it into a file (called keep_mouse_missing.sh) and make it executable using the chmod +x <file> command.

#!/bin/bash

# Bash script to keep mouse pointer moving so suspend does not occur.
#
# The mouse pointer will move around the current position of the mouse
# by using the xdotool tool to move the mouse.
# 
# To move the mouse around the center of the screen change mousemove_relative
# to mousemove in the xdotool command.

# The LENGTH denotes how far the mouse pointer will move at once.
LENGTH=${1:-1}

# The DELAY is the number os seconds between each move of the mouse.
DELAY=${2:-5}

echo "Moving mouse with a length of $LENGTH and a delay of $DELAY." 

while true
do
    for ANGLE in 0 90 180 270
    do
        xdotool mousemove_relative --polar $ANGLE $LENGTH
        sleep $DELAY
    done
done

Use the script in the following way.

./keep_mouse_moving.sh

You can pass two parameters to the script to change the length and delay, which default to.

  • length = 1 - This is the amount of movement that the mouse will do. Set this higher to make the mouse move more.
  • delay = 5 - The number of seconds to delay between mouse moves.

You can pass the parameters to the script like this.

./keep_mouse_moving.sh 1 5

This will run until you quit it.

Add new comment

The content of this field is kept private and will not be shown publicly.