How to write a shell script to find PID and Kill - linux

I am trying to write a script that looks for the PID of another script that ran previously. Once it finds that PID it then sends a kill signal.
I can find the PID but the kill signal does not process, I get a return message that it is not a PID.
Here is what I am doing:
#!/bin/bash
PID=`ps -eaf | grep "start.sh" | awk '{print $2}'`
echo "$PID"
if [[ -z "$PID" ]];
then(
echo "Start script is not running!"
)else(
kill -9 $PID
)fi
The script it is trying to kill starts many other scripts so I am hoping that killing start.sh will kill all child processes.

When you run
ps -eaf | grep "start.sh" | awk '{print $2}'
you create a subshell with the word start.sh in it. grep will then pick up on its own process and the start.sh one so you will get two PIDs back.
This means when you are trying to kill both start.sh and the
ps -eaf | grep "start.sh" | awk '{print $2}'
processes. The start.sh will die but the other will no longer exist so can't be killed, so it gives you an error.
If you were to split up the commands you might have better luck:
PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "start.sh" | awk '{print $2}')

Try using pgrep:
PID=$(pgrep yourprocesname)

You could use pidof rather then ps -ef
PID=`pidof start.sh | awk '{print $1}'`
echo "$PID csshst will be stopped"
if [[ -z "$PID" ]]; then
(
kill -9 $PID
)
fi

Here is another solution that may be useful for someone:
#!/bin/bash
kill-process-by-name(){
processes=$(ps -A)
kill -9 `echo "$processes" | grep "$1" | cut -f2 -d" "`
}
kill-process-by-name "start.sh"

trashPID=`pidof start.sh | awk '{print $1}'`
echo "$PID csshst will be stopped"
if [[ -z "$PID" ]]; then
(
kill -9 $PID
)
fi
purely

Related

before executing script.sh, Check script.sh running or not, if its running kill and continue

CMD# bash script.sh
#!/bin/bash
PRE_CHECK=$0
PROCESS_ID=`ps -ef | grep "$PRE_CHECK" | egrep -v 'grep' | awk '{print $2}'`
[[ ! -z $PROCESS_ID ]] && kill -9 $PROCESS_ID
echo ""
echo ""
echo ""
In order to know this, you need to know your own process ID. This is stored in the variable $$, so you need to kill all PIDs, except for that one.

Don't kill created processes, which created by ps - linux

give some advice, please.
I am trying to kill processes remotely (ssh to hostname), find some processes and kill them. But I have a condition: Do not kill java process, sshd and gnome.
Here is example (I just do echo except kill):
#/bin/sh -x.
HOSTFILE=$1
vars=`cat $HOSTFILE`
for i in $vars; do
ssh "$i" /bin/bash <<'EOF'
echo $(hostname)
ps aux | grep -e '^sys_ctl'| grep -v "java" | grep -v "sshd" | \
grep -v "gnome" | awk '{print $2$11}'| for i in `xargs echo`; do echo $i; done;
EOF
done
The result is:
host1:
21707/bin/bash
21717ps
21718grep
21722awk
21723/bin/bash
21724xargs
host2:
15241/bin/bash
15251ps
15252grep
15256awk
15257/bin/bash
15258xargs
89740-bash
98467sleep
98469sleep
98471sleep
98472sleep
98474sleep
98475sleep
I want to kill (output), only sleep processes, not grep,awk,bash,xargs,ps
Can you suggest something elegant?
why not just : kill $(pgrep -f sleep)
or : pkill -f sleep

My bash script won't execute commands after kill command

I am trying to make a bash script that is killing a process and then it's going to do other stuff.
PID=`ps -ef | grep logstash | grep -v "grep" | awk '{print $2}'`
echo $PID
kill -9 $PID
echo "logstash process is stopped"
rm /home/user/test.csv
echo "test.csv is deleted."
rm /home/example.txt
echo "example.txt is deleted."
When I run the script, it kills logstash as exptected but it terminates also my whole script.
I've also tried: kill -9 $(ps aux | grep 'logstash' | awk '{print $2}').
With this command, my script will be terminated as well.
it looks like your script name includes "logstash".
As a consequence, PID is filled with 2 values, and the kill command kills your script as well.
Rename your script without "logstash" in the name should fix the issue.
This should correct your issue :
PID=$( ps -ef | grep -E '[ ]logstash[ ]' | grep -v "grep" | head -1 | awk '{print $2}')
echo $PID
kill -9 $PID
echo "logstash process is stopped"
rm /home/user/test.csv
echo "test.csv is deleted."
rm /home/example.txt
echo "example.txt is deleted."
Regards!

Issue finding the process id from shell scipt

mySample.sh
pid=$(ps -Af | grep $1 | grep -v grep | awk ' { print $2 } ');
echo $pid
The above command is printing and killing the temporary process that was created for grep
Even though i do not have any process running with Abcd,
This is printing pid
Any ways to ignore it,
iam actually ignoring it using grep -v, still...
./mySample.sh Abcd
6251 6378 6379
Any Issue in fetching the process id.?
Basic command line output is below,After running a process with name Acc_Application_One
[root#localhost Desktop]# ps -Af | grep Acc
root 6251 2758 0 16:16 pts/1 00:00:00 ./Acc_Application_One
root 7288 2758 0 16:57 pts/1 00:00:00 grep Acc
Changed mySample.sh
pgrep -fl "$1"
And the output is
[root#localhost Desktop]# mySample.sh Acc_Application_One
6251 7289
To kill a process with the pattern anywhere in command line use pkill -f:
pkill -f "$1"
As per man pkill:
-f Match the pattern anywhere in the full argument string of the process instead of just the executable name.
Similarly you can use pgrep -f "$1" to list the process id of the matching process.
Try something much simpler:
pid=$(pgrep "$1")
And if you want to kill it:
pkill "$1"
The problem will become clear when you remove the awk: mySample.sh will have Abcd as well.
ps -Af | grep " $1" | grep -Ev "grep|$0" | awk ' { print $2 } '
Changed mySample.sh script with below code
And This is just fetching the processId using the parameter sent
and killing it
pid=$(pgrep -fl $1 | grep -v '[k]ill_script'| awk ' { print $1 } ')
echo $pid
if [[ -n ${pid} ]]; then
echo "Stopping Acc Application $1 with pid=${pid}"
kill -9 ${pid}
fi
Thanks

Linux kill - Do not exit program

I am killing a process inside a script using kill -9 command. The process gets killed but control is exiting from the script. How do i make the following statements work after the kill command?.
kill -9 `ps -ef | grep /home/myFile | grep -v grep | awk {'print $2'}`
sleep 5
echo Process Stopped
Here both sleep and echo are not working. Can some one help?
if you have pkill installed in your machine, you should use it
$ pkill -9 -f /home/myFile
if not, may be you can use the ancient trick instead of grep -v grep
$ kill -9 $(ps -ef | grep '[/]home/myFile' | awk {'print $2'})
The trick is [/]home/myFile.
grep'ing [/]home/myFile matches to /home/myFile,
but argument shown in ps does not contain grep /home/myFile.

Resources