How to move a mouse pointer automatically in Linux to simulate user activity
August 6, 2018 4 Comments
My various Linux installations all have Suspend to RAM enabled with a specified timeout. Sometimes I want to override the timeout; for example if I have left something running in a terminal window or I have left the package manager in a virtual machine upgrading the guest installation. I could of course launch the system’s power manager GUI and temporarily disable Suspend to RAM or increase the timeout, but I prefer to use a shell script, launched by double-clicking on a Desktop icon, to move the mouse pointer automatically to fool the OS into believing someone is using the machine. There are various ways of doing this, but the method I prefer is given below.
1. Create a Bash script ‘/home/fitzcarraldo/keep_mouse_moving.sh
‘ containing the following:
#!/bin/bash # # Script to keep mouse pointer moving so that, for example, Suspend to RAM timeout does not occur. # # The mouse pointer will move around its current position on the screen, i.e. around any position # on the screen where you place the pointer. However, if you prefer it to move around the centre # of the screen then change mousemove_relative to mousemove in the xdotool command below. # # Set LENGTH to 0 if you do not want the mouse pointer to actually move. # Set LENGTH to 1 if you want the mouse pointer to move just a tiny fraction. # Set LENGTH to e.g. 100 if you want to see more easily the mouse pointer move. LENGTH=1 # # Set DELAY to the desired number of seconds between each move of the mouse pointer. DELAY=5 # while true do for ANGLE in 0 90 180 270 do xdotool mousemove_relative --polar $ANGLE $LENGTH sleep $DELAY done done
Do not forget to make the script executable.
As you can see in the above script, it is possible to control how much, if at all, the mouse pointer actually moves on the screen. While the script is running you are not precluded from moving the mouse manually as well.
If you only want to prevent the OS from suspending while music is playing or you are watching a video (with audio), then a simple modification to the script can achieve that:
#!/bin/bash # # Script to keep mouse pointer moving while audio is playing so that, for example, Suspend to RAM timeout # does not occur while listening to music or watching a video (with sound). # # The mouse pointer will move around its current position on the screen, i.e. around any position # on the screen where you place the pointer. However, if you prefer it to move around the centre # of the screen then change mousemove_relative to mousemove in the xdotool command below. # # Set LENGTH to 0 if you do not want the mouse pointer to move. # Set LENGTH to 1 if you want the mouse pointer to move just a tiny fraction. # Set LENGTH to e.g. 100 if you want to see more easily the mouse pointer move. LENGTH=1 # # Set DELAY to the desired number of seconds between each move of the mouse pointer. DELAY=5 # while true do if [[ `cat /proc/asound/card*/pcm*/sub*/status | grep RUNNING | wc -l` -ne 0 ]]; then for ANGLE in 0 90 180 270 do xdotool mousemove_relative --polar $ANGLE $LENGTH sleep $DELAY done fi done
2. Create a Desktop Configuration File ‘/home/fitzcarraldo/Desktop/keep_mouse_moving.desktop
‘ containing the following:
[Desktop Entry] Comment[en_GB]=Keep mouse moving automatically Comment=Keep mouse moving automatically Exec=xterm -iconic -e "bash -c /home/fitzcarraldo/keep_mouse_moving.sh" GenericName[en_GB]=Keep mouse moving automatically GenericName=Keep mouse moving automatically Icon=input-mouse MimeType= Name[en_GB]=keep_mouse_moving Name=keep_mouse_moving Path=/home/fitzcarraldo StartupNotify=true Terminal=false TerminalOptions=\s--noclose Type=Application X-DBUS-ServiceName= X-DBUS-StartupType=none X-LXDE-SubstituteUID=false X-LXDE-Username=fitzcarraldo
(In installations that use KDE or GNOME I replace the ‘LXDE
‘ above with ‘KDE
‘ or ‘GNOME
‘, as appropriate.)
3. Use the distribution’s package manager to install xterm
and xdotool if they are not already installed.
4. Whenever I want to fool the OS into thinking a user is moving the mouse, I double-click on the mouse icon on the Desktop and the Bash script is launched in a minimised xterm window, which I can see on the Panel. I can then leave the installation knowing that it will not suspend to RAM and that the screensaver will not kick in. When I want to stop the mouse pointer moving automatically, I simply click on the xterm bar on the Panel to open the xterm window, and click on Close (×) on the window’s title bar to terminate xterm
and the shell script.
Note that the X Windows Toolkit option -iconic
may not work in some Desktop Environments (GNOME, for example), in which case you can minimise the xterm
window manually if you want, or use another terminal emulator.
By the way, if you use GNOME and it is currently configured not to display icons on the Desktop, you can change this by using the following command:
user $ gsettings set org.gnome.desktop.background show-desktop-icons true
Of course you are not obliged to have the .desktop
file on the Desktop; it could be in any directory.
Pingback: How to stop inactive user sessions triggering Suspend to RAM in a single-seat, multi-user installation of Lubuntu 18.04 | Fitzcarraldo's Blog
nice trick , it helped me greatly !!!
Thanks !!!
You can also install keep-presence from snap:
sudo snap install keep-presence
more info: https://github.com/carrot69/keep-presence
Pingback: Moving from Lubuntu 18.04 to 20.10 | Fitzcarraldo's Blog