0

I used xdotool to automate some actions. It takes data from a spreadsheet, and "types" it into a Web site, instead of me having to manually copy and paste all of that data over, saving hundreds of hours of work. This is running through a simple while loop, and the data extracted using awk from the spreadsheet.

The problem is, sometimes either while testing out a new xdotool Bash script, or just in some random cases, xdotool starts to wreck havoc on things, and it is difficult to stop. I have to switch back to the terminal window, and hit CTRL+C quickly, but in the meantime, it can do some damaging things, such as type in potentially dangerous code in the terminal that mistakenly gets executed. The most serious issues I've actually had is that it takes over and I can't regain control of things.

Is there some way to integrate, perhaps into the Bash script, some kind of "kill switch", perhaps for instance, if I hit a certain key combo on the keyboard, it tells it to immediately stop running?

Village
  • 5,035
  • 15
  • 50
  • 84
  • 1
    Would creating a new hotkey in your WM with the command pkill xdotool sound like a reasonable solution to your problem? – undercat Aug 23 '19 at 02:09
  • I don't know that would be enough, because the while loop keeps making new instances of xdotool. – Village Aug 24 '19 at 03:23
  • You have a point. I've written an answer working around this by suggesting to use a separate bash shell instance, but that solution is not quite as transparent as I had originally anticipated. – undercat Aug 24 '19 at 10:05

2 Answers2

1

Create a shortcut in the window manager of your choice that calls the following command:

pkill -f xdotool

That will kill all processes that have xdotool in their command line which in most use cases should be an accurate enough kill switch.

Then, whenever you are about to run a potentially dangerous script, do it from another shell instance:

bash -c 'while true; do xdotool ...; done'

This way, whenever the script goes out of control, you will be able to kill it with the shortcut.

undercat
  • 1,857
0

First, when running experimental scripts, don't let them modify production data. Point the scripts to test data/URLs where they can't do real damage.

Second, you could wrap the invocation of xdotool within an if statement so that it only runs if some file doesn't exist. To stop all instances of xdotool, you just need to create this file. Here is a sample written in Perl that can be easily translated to your favorite scripting language.

run-tool.pl

#!/usr/bin/perl
use strict;

my $stop_file = "/tmp/dont-run-xdotool.txt"; if (! -e $stop_file) { # run xdotool here } exit 0;

David Levner
  • 155
  • 1
  • 4