Wolfgang Michael
Wolfgang Michael

Reputation: 1

Killing a running sh script using pid

I have made an Auto Clicker and was wondering how i would kill it using kill [pid]

My auto Clicker works like this:

while true [1]; do
   xdotool click --repeat 10000 --delay 150 1
done

code I have used to try and terminate running proccess:

ps -ef | grep AutoClicker | grep -v grep | xargs kill -9 

I found this code on another post, however i have had no luck with it.

Upvotes: 0

Views: 112

Answers (3)

Eric Bolinger
Eric Bolinger

Reputation: 2932

Instead of searching for the process ID, use your own "PID file". I'll demonstrate with sleep in place of xdotool.

echo $$ > /tmp/my.pid

while true [1]; do
   echo "clicking"
   sleep 5
done

# rm -f /tmp/my.pid

I can run this script in another terminal window or the background. The important thing is that /tmp/my.pid contains the process ID of this running script. You can stop it with:

kill $(</tmp/my.pid)

This interrupts the while-loop. Need to figure out how to remove the PID file...

Upvotes: 0

jhnc
jhnc

Reputation: 16868

If you run your code:

ps -ef | grep AutoClicker | grep -v grep | xargs kill -9 

you should get an error message from kill.

kill expects a list of process ids not usernames, times, and random strings.

You need to filter out everything except the pids with something like:

ps -ef | grep AutoClicker | grep -v grep | awk '{print $2}' | xargs kill

Upvotes: 1

nullPointer
nullPointer

Reputation: 4574

pkill -f AutoClicker or kill $(pgrep -f AutoClicker)

Upvotes: 1

Related Questions