Hello there long history short: I'm trying to create a script to run through CI server to kill a specific process.
So searching over the internet I found out this command:
myuser@server:/home/myuser# ps -ef | grep someUniqueText | grep -v grep | awk '{print $2}'
5263
When I run it in the console it returns me exactly what I need (5263
), so I created this bash:
#!/bin/bash
PROCESSNAME=$1
PID=$(ps -ef | grep $PROCESSNAME | grep -v grep | awk '{print $2}');
echo $PID;
When I execute it as:
myuser@server:/home/myuser# ./killProcess.sh somename
5263 4587 1236
The bash script returns 3 numbers being the first the process I need and I have no idea what the other two are, Why? o.O
PS.: I'm using echo
to test it first, I will change it to kill -9
killall
tool?somename
is also part of your commandline.pgrep
andpkill
$0
– which is the script's name as you call it, i.e. including the./
part, if present:PID=$(ps -ef | grep $PROCESSNAME | grep -v grep | grep -v $0 | awk '{print $2}');
. Butkillall somename
would also work. There's even a preview:killall -s 0 -v somename
. It "sends" signal 0 which simply checks whether the process exists but doesn't actually send a signal. The process won't even notice.