I'm trying to setup a task to kill certain server processes when the server gets into a weird state such as when it fails to boot one process, but another process gets keeps running and so not everything boots up. This is mainly a task for development so you can do jake killall to kill all processes associated with this project.
I'm having trouble figuring out how to get the pid after doing: ps aux | grep [p]rocess\ name | {HOW DO I GET THE PID NOW?} and then after getting the ID how do I pass that to kill -9 {PID HERE}
The PID is the second column, so you can do
ps aux | grep [p]rocess\ name | awk '{print $2}'
All together,
my_pid=$(ps aux | grep [p]rocess\ name | awk '{print $2}')
kill -9 $my_pid
You could also you killall <program> or pkill <program> or pgrep <program>
Related
Find specific client with name and memory
Get the client pId
Kill pId
Can I do kill that process just within the terminal.
What I tried
ps aux | grep leagueClientUxHelper | awk '{print $2}'
but I can't get the large memory process.
Same process names in activated status:
You can kill a process with the id by using the kill command.
ps aux | grep TextEdit | awk '{print $2}' | xargs kill
That will pipe it directly into the kill command. You will get an error because the ps aux will show two PIDs but it won't cause any problems.
Kill only works with PIDs
Are you trying to drill down to a certain memory length or compared to another instance like league1 memory is > leage2 memory?
I want to kill all processes named ruby. How do I do that in one go, without manually doing kill -9 PID for every process?
A simple oneliner should do the trick for you:
ps aux | grep [s]lack | awk '{print $2}' | xargs kill -9
ps aux -> list all processes
grep -> search for a particular process by name or any identifier
awk '{print $2}' -> get the 2nd column entries from the result of grep
xargs kill -9 -> kill the processes.
killall command may solve your problem. Please check the manual for details.
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.
pkill -f AutoClicker or kill $(pgrep -f AutoClicker)
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
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...
I was running an ubuntu console, when I type the following command, all the processes would be perfectly killed.
kill -9 $(ps -ef | grep 'job1/' | grep -v grep| awk '{print $2}')
But when I was trying to use crontab to call a script routinely, things went wrong.
#!/bin/bash
pid=$(ps -ef | grep 'job1/' | grep -v grep | awk '{print $2}')
echo $pid
kill -9 $pid
# the following commands were never executed
sleep 5
/data/job1/tomcat8/bin/startup.sh
The result was just like this:
15432 15438
Killed
It seems to just killed the job, but won't execute the following commands. Any idea?
If you are going to make a script that kills things by PID then you need to be very careful that you kill the right things.
You already have grep -v grep to avoid killing the grep itself, but it seems that you have not put in anything to protect against the script killing itself. Since you know your own PID you could grep -v that, but what if you are 123 and one of the things you want to kill is 1234? Probably safer to go by script name.
I'm putting in a reaper line into a rake task to kill some additionally spawned ruby tasks as they somehow creep up on occasion.
system "ps aux | grep 'namespace:taskname' | grep ruby | grep -v grep | awk '{print $2}' | xargs kill -9; echo 'Reaped old namespace:taskname processes.'"
I'd like to add grep -v $PID_OF_CURRENT_TASK in that just to be sure I don't kill the current task that's running as well.
How do I get that PID?
You get the current PID in Ruby with Process.pid