'How to terminate a script running in the background with a key press?
I have a very simple script that simulates a key press every 30 seconds which I use to automate resource collection in an idle game while I am away from my computer. The script looks like this:
#!/bin/bash
while true; do
xdotool keydown a click 1 keyup a
sleep 30
done
I want to be able to stop the script when I press some key (i.e. "x"). I have tried doing that using read, but that only works for me if I am typing into the terminal. What I want is to be able to click the key ("x") from anywhere to terminate the script, since when the script is running the idle game is in focus, not the terminal. How could I do this?
Solution 1:[1]
I ended up just creating a keyboard shortcut that runs a command that checks if the script is running and killing it with pkill -f script.sh.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | taway0282 |
