Linux kill process by cmdline (multiple with same name) - bash

I am writing a bash-file (.command #OS X) to easily update a program at my remote server (Linux). I am stuck trying to figure out which PID to kill. I have different processes with the same name, which is mono, since they are executed by mono.
Using pgrep mono I get all the PIDs using mono, however, I only want to kill the process that has the command line SCREEN -dmSL steambot mono SteamBot.exe.
How do I know what PID to kill? Atm, my .command file looks like this:
ssh xxx#xxxx.com
pgrep mono
I am using ssh-keychain to login also.

like this:
ps -ef | grep '[S]CREEN -dmSL steambot mono SteamBot\.exe' |
awk '{print $2}' | xargs -r kill

Related

How do I make a stop after a run?

If I do a:
npm run script
Can I stop it with a stop?
npm stop script
Why I tried it and it does not work.
I know that with the combination of "Ctrl + c" I kill it, but I want to do it by command.
Try something like that:
ps -ef | grep script | awk '{print $2}' | head -n1 | xargs kill -9
This command should find first process named script on the list of all unix processes created by all users and kill it by with using its PID.

restrict pidof to own processes

In bash,
pidof unison
will list me unison processes of all users. However I am only interested in instances started under my uid.
How can I achieve this?
What I want to do:
I'm periodically syncing several accounts on several machines using unison. However, I want the sync to not start, if the current user has manually started unison.
You could use pgrep instead, like
pgrep -u <userid> unison
which will return a list of pids of unison processes that have the euid of the given user.
You can parse ps output like this:
ps -ef | grep unison | grep -v grep | grep "${USER}" | awk '{print $2}'

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

How to get the process id (PID) of an app launched using `open` command on OSX?

I've launched an application via the open command on the OSX command line like so:
open -a "/Applications/Adobe After Effects CC/Adobe After Effects CC.app"
I want to get the process id of that launched application. Is there any way to do this reliably on OSX? It doesn't seem open returns anything, so I'm not sure I can even pipe its result into something like ps to perform a grep operation. I thought that maybe since the app is launched via the terminal I would know which app is the frontmost, but am doubting the reliability of that solution. Any ideas?
After executing open -a, you can execute ps command. Doing a grep on the output of ps command gives info on process ID.
ps aux | grep -v grep |grep -i <application name> | awk '{print $2;}'
The one given below gives the elapsed time for the process.
ps aux -o etime,command | grep -v grep |grep -i <application name> | awk '{print $2, $12,$13}'
We can compare the elapsed time to know the pid of the recently launched one.
These days, pgrep -n $APPLICATION_NAME seems like the easiest way to accomplish this. From the man page:
-n Select only the newest (most recently started) of the matching processes.
-o Select only the oldest (least recently started) of the matching processes.
Activity Moniter
You can use Activity Moniter. Located in /System/Applications/Utilities/Activity\ Monitor.app
Open the app, hit the search bar on the top and search for the app you want to get the PID of.

Killing processes SHELL

This command ps -ef | grep php returns a list of processes
I want to kill in one command or with a shell script all those processes
Thanks
The easiest way to kill all commands with a given name is to use killall:
killall php
Note, this only sends an interrupt signal. This should be enough if the processes are behaving. If they're not dying from that, you can forcibly kill them using
killall -9 php
The normal way to do this is to use xargs as in ps -ef | grep php | xargs kill, but there are several ways to do this.
ps -ef lists all processes and then you use grep to pick a few lines that mention "php". This means that also commands that have "php" as part of their command line will match, and be killed. If you really want to match the command (and not the arguments as well), it is probably better to use pgrep php.
You can use a shell backtick to provide the output of a command as arguments to another command, as in
kill `pgrep php`
If you want to kill processes only, there is a command pkill that matches a pattern to the command. This can not be used if you want to do something else with the processes though. This means that if you want to kill all processes where the command contain "php", you can do this using pkill php.
Hope this helps.
You can find its pid (it's on the first column ps prints) and use the kill command to forcibly kill it:
kill -9 <pid you found>
Use xargs:
ps -ef | grep php | grep -v grep | awk '{print $2}' | xargs kill -9
grep -v grep is exclude the command itself and awk gives the list of PIDs which are then passed kill command.
Use pkill php. More on this topic in this similar question: How can I kill a process by name instead of PID?

Resources