Skip to content

Instantly share code, notes, and snippets.

@imshaiknasir
Last active October 25, 2023 18:34
Show Gist options
  • Save imshaiknasir/07131452a44d2c7952c3c176c130773a to your computer and use it in GitHub Desktop.
Save imshaiknasir/07131452a44d2c7952c3c176c130773a to your computer and use it in GitHub Desktop.
Script and setup procedure to move mouse cursor every minute.

Script and setup procedure to move mouse cursor every minute.

1. Setting up the script.

Prerequisite: xdotool

To install xdotool : sudo apt-get install xdotool

Create a script file nano moveCustor.sh and save it in your desired directory. (Here, I've saved it in my /home/user directory)

Paste the below script content in the file.

Remember to make your script executable using the command chmod +x moveCustor.sh. Then you can run your script with ./moveCustor.sh.

Please note that xdotool might not work as expected in all environments or for all types of applications, especially those that don’t use standard X11 interfaces. If you encounter any issues or need more complex functionality, I recommend checking out the xdotool man page or other online resources for more information.

2. Make the script run automatically when system is logged-in.

  • Create a new service file in /etc/systemd/system/ with a .service extension, for example myscript.service. You can use sudo nano /etc/systemd/system/myscript.service to open a new file in the nano text editor.

  • Add the following content:

    [Unit]
    Description=My Script
    
    [Service]
    User=yourUserName
    Environment=DISPLAY=:1
    Environment=XAUTHORITY=/run/user/1000/gdm/Xauthority
    ExecStart=/path/to/your/script.sh
    
    [Install]
    WantedBy=multi-user.target

    Note:

    • To get your Environment=DISPLAY value, do echo $DISPLAY.
    • To get your Environment=XAUTHORITY value, do echo $XAUTHORITY.
    • Replace yourusername with your actual username.
  • Save and close the file (in nano, you can do this by pressing Ctrl+X, then Y to confirm saving).

  • Now you need to reload the systemd manager configuration with: sudo systemctl daemon-reload.

  • Enable your service to be run at startup: sudo systemctl enable myscript.service

  • You can start your service immediately with: sudo systemctl start myscript.service

  • You can check the status of your service anytime with: sudo systemctl status myscript.service

#!/bin/bash
while true; do
# Get the idle time in milliseconds.
idle_time=$(xprintidle)
# Check if the idle time is greater than 1 minutes (60000 milliseconds).
if [ $idle_time -gt 60000 ]; then
# Generate two random numbers between 1 and 1000.
x=$((RANDOM % 1000 + 1))
y=$((RANDOM % 1000 + 1))
# Move the mouse to the random position.
xdotool mousemove $x $y
# Reset the idle time counter.
xdotool key ctrl
fi
# Wait for 5 seconds before checking the idle time again.
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment