How to get and kill the process Mac terminal - macos

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?

Related

CodeDeploy fails in ApplcationStop coz not able to Kill process

I have been using the same script and it used to work before. Due to another error previously,
I deleted the application folder from the console. (The application was killed successfully in the previous deploy but there was an error in AfterInstall)
stop_bot.sh:
#!bin/bash
ps -ef | grep testMain.py | grep -v grep | awk '{print $2}' | xargs kill -9
Error:
LifecycleEvent - ApplicationStop
Script - scripts/stop_bot.sh
[stderr]kill: not enough arguments
The error means that grep testMain.py does not return any values. You have to double check your programs and ensure that grep testMain.py (or whatever it should be) actually provides a list of processes to be killed.

How to kill running processes by name from the terminal

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.

Kill command won't work correctly in bash script

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.

Bash function to kill process

I made an alias for this function in order to kill processes in bash:
On my .bashrc file
kill_process(){
# $1 being a parameter for the process name
kill $(ps ax | grep "$1" | awk '{print $1}')
}
alias kill_process=kill_process
So, suppose I want to kill the meteor process:
Let's see all meteor processes:
ps aux | grep 'meteor' | awk '{print $2}'
21565
21602
21575
21546
Calling the kill_process function with the alias
kill_process meteor
bash: kill: (21612) - No such process
So, the kill_process function effectively terminates the meteor processes, but it's kill command looks for an inexistent pid. Notice the pid 21612 wasn't listed by ps aux | grep. Any ideas to improve the kill_process function to avoid this?
I think in your case the killall command would do what you want:
killall NAME
The standard way of killing processes by name is using killall, as Swoogan suggests in his answer.
As to your kill_process function, the grep expression that filters ps will match the very own grep process (you can see this running the pipeline without awk), but by the time kill is invoked, that process is no longer running. That's the message you see.
Each time you run the command, grep runs again with a new PID: that's the reason you can't find it on the list when you test it.
You could:
Run ps first, pipe it into a file or variable, then grep
Filter grep's PID out of the list
(Simpler) supress kill output:
kill $(...) 2>/dev/null

ps aux auto close app

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>

Resources