AIX - How to kill using process name instead of PID - process

Is there a way to kill a process by specifying the process name instead of PID for AIX?
E.g. for the below process I want to kill it by specifying sapstartsrv instead of 10682424
hmsadm 10682424 1 0 Apr 30 - 0:54 /usr/sap/HMS/ASCS01/exe/sapstartsrv pf=/usr/sap/HMS/SYS/profile/START_ASCS01_H\
Thanks.

You can use command like this:
kill -9 $(ps -ef|grep sapstartsrv|awk '{print $2}')
of course first check if command ps -ef|grep sapstartsrv|awk '{print $2}' return only processes you want to kill

Try this. The brackets around the first letter of process you are trying to kill helps. Obviously change this to a valid server.
while true; do date; ping -c4 server; sleep 500; done &
ps -aef | grep -i [p]ing | awk '{print $2}' | xargs kill -9
If that doesn't work sometimes you have to kill the parent process.
ps -aef | grep -i [p]ing | awk '{print $2 " " $3}' | xargs kill -9

Related

Force write to Xcode 'Debugger Output' in console?

The Xcode console has a 'Debugger output' filter. I understand this is for use with lldb, and that you can get messages to print to this output by using breakpoints. My question is not how to do that.
My question is: what is the underlying mechanism Xcode itself uses to write lldb messages to Debugger Output (not Target Output)? Is there a variable similar to stdout or stderr that writes here? Is it possible, from Xcode target code (Swift/Obj-C/C), to write to this output?
Looks like Xcode uses a tty to communicate with lldb, and you can interface with the Debugger Output using that:
echo "Wheeeeeeee" > $(lsof -p $(ps -A | grep -m1 MacOS/Xcode | awk '{print $1}') | grep -m2 dev/ttys | tail -1 | awk '{print $9}')
Breaking the above down:
$ ps -A | grep -m1 MacOS/Xcode | awk '{print $1}'
21280
This gives the process ID of Xcode (21280). Using this, we can find the files it has open:
$ lsof -p 21280 | grep /dev/ttys
Xcode 21280 tres 47u CHR 16,3 0t0 3569 /dev/ttys003
Xcode 21280 tres 58u CHR 16,5 0t0 3575 /dev/ttys005
The one with the highest number (/dev/ttys005 in this case) is the one we want, so let's extract it. tail -1 will give us the last line of output, and awk '{print $9}' will give us the 9th item on the line, which is what we want!
$ lsof -p 21280 | grep /dev/ttys | tail -1 | awk '{print $9}'
/dev/ttys005
Now we can use this to write whatever we want:

Efficient way to quit Apps/process on OS X

Which would be the best way to quit apps by app name on OS X?
Either I can use Apple script
NSString *scriptSource = [NSString stringWithFormat:#"tell application \"%#\" to quit",processName];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptSource];
[script executeAndReturnError:nil];
Else I can go ahead with NSTask to execute ps command, do grep and awk to retrieve pid of the process and then quit the process using pid, like what is given below.
ps aux | grep -v grep |grep <process name> | awk '{print $2}'
In this case I end up using NSTask 4 times + 1 more time to kill the process.
Which is more efficient in terms of performance?
Pipe the output to xargs command like below.
ps aux | grep -v grep |grep <process name> | awk '{print $2}' | xargs kill
OR
ps aux | grep -v grep | awk '/process name/{print $2}' | xargs kill
This would kill all the 4 processes.

Counting number of processes using AWK

I'm trying to come up with command using AWK which will list down all the processes along with its number of instances running:
I'm using following command
ps axo pid,command | awk -F/ '{print $1, $4}'
and I;m getting following result
1727 sshd
1807 httpd
1834 abrtd
1842 abrt-dump-oops -d abrt -rwx
1848 httpd
1849 httpd
1879 gpm -m
I want to above command so that it can display total number of process count along with the process, something as follows
1 1727 sshd
3 1807 httpd
1 1834 abrtd
1 1842 abrt-dump-oops -d abrt -rwx
1 1879 gpm -m
In fact I want to kill a process running more than 5 instances does not matter what process it is.
This is not awk but it should produce the output you want.
ps axo pid,command | sort -k2 | uniq -c -f 1
try this .....
ps -eo command | grep -v COMMAND | awk '{count[$0]++}END{for(j in count) print count[j] ,j}' | sort -rn | head

How to kill a process in cygwin?

Hi i have the following process which i cant kill:
I am running cygwin in windows xp 32 bit.
I have tried issuing the following commands:
/bin/kill -f 4760
/bin/kill -9 5000
kill -9 5000
kill 5000
When i write /bin/kill -f 4760 i get the message, 'kill: couldn't open pid 4760'.
When i write /bin/kill -9 5000 i get the message, 'kill: 5000: No such process'.
I simply don't understand why this process cant be killed.
Since it has a WINID shouldnt it be killed by /bin/kill -f 4760?
hope someone can help thx :)
The process is locked from Windows most likely. The error you are getting "couldnt open PID XXX" points to this.
To confirm try killing it with windows taskkill
taskkill /PID 4760
Strangely, the following works in Cygwin:
echo PID1 PID2 PID3 | xargs kill -f
For example:
ps -W | grep WindowsPooPoo | awk '{print $1}' | while read line; do echo $line | xargs kill -f; done;
Different Windows programs will handle the signals that kill sends differently; they've never been designed to deal with them in the same way that Linux/Cygwin programs are.
The only reliable method for killing a Windows program is to use a Windows specific tool, such as Task Manager or Process Explorer.
That said, if you've not already, you may have luck with running your Cygwin terminal in administrator mode (right click on your shortcut and select "Run as administrator").
The method presented by #Donal Tobin is correct:
kill -f <pid>
However, I don't need to log in as administrator.
Create a file called killall.sh with this line
ps -W | grep $1 | awk '{print $1}' | while read line; do echo $line | xargs kill -f; done;
Then give it execute permissions.
chmod 777 killall.sh
In your .bash_profile add this line
alias killall="~/killall.sh" (point it to the correct location)
Then you just have to type "killall [name]"
killall.sh - Kill by process name.
#/bin/bash
ps -W | grep "$1" | awk '{print $1}' | xargs kill -f;
Usage:
$ killall <process name>
For me this command does not work on Windows 10 in Cygwin:
$ kill -f 15916
bash: kill: (15916) - No such process
Instead of it, you can use next commands:
$ powershell kill -f 15916
$ netstat -ano | grep ':8080' | awk '{print $5}' | xargs powershell kill -f
$ netstat -ano | grep ':8080' | awk '{print $5}' | while read pid; do powershell kill -f $pid; done;
$ netstat -ano | grep ':8080' | awk '{sub(/\r/,"",$5) ; print $5}' | while read pid; do taskkill /F /PID $pid; done;
SUCCESS: The process with PID 15916 has been terminated.

ubuntu shell scripting

I'm new to shell scripting. I need to write a script that executes this command to get the process ID's for the tasks...
ps aux | grep java | grep dbConvert2 | awk '{print $2}'
then do some other stuff, and then kill the process ID's that I grabbed earlier...
I know I can do kill -9, i just don't know how to dynamically grab all the PID's and store them as variables
append | xargs kill -9 to your current command
[edit]
if you want to do some operations on each id, you can use a for loop, something like:
for my_pid in `YOUR_CMD`; do
<some stuff with $my_pid>
kill -9 $my_pid
done
pkill -9 'java.*dbConvert2'
You might want to use pgrep 'pattern' to try different patterns before.
Edit: If your process isn't matched, you might need to use -f (applies to both pgrep and pkill, use after the -9 though) to search the entire command including arguments.
Example: pkill -9 -f 'java.*dbConvert2'