Don't kill created processes, which created by ps - linux - 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

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.

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!

Bash script kill command in for loop

I want to kill all processes containing some string. I wrote script for doing this. However, when I execute it, it gets "Killed" signal after first iteration of for loop. This is my code:
#!/bin/bash
executeCommand () {
local pname="$1";
echo $HOSTNAME;
local search_terms=($(ps aux | grep $pname | awk '{print $2}'))
for pros in "${search_terms[#]}"; do
kill -9 "$pros"
echo $pros
done
exit
}
executeCommand "$1" # get the string that process to be killed contains
I execute it like ./my_script.sh zookeeper.
When I delete the line containing kill command, for loop executes until end, otherwise, after first kill command, I get as an output "Killed" and program exits.
What is possible reason for this, and any other solution to reach my goal?
The silly (faulty, buggy) way to do this is to add grep -v grep to your pipeline:
# ${0##*/} expands to the name of the running script
# ...thus, we avoid killing either grep, or the script itself
ps aux | grep -e "$pname" | egrep -v "grep|${0##*/}" | awk '{print $2}'
The better way is to use a tool built for the job:
# pkill already, automatically, avoids killing any of its parent processes
pkill "$pname"
That said, matching processes by name is a bad practice to start with -- you'll also kill less yourproc.log or vim yourproc.conf, not just yourproc. Don't do it; instead, use a proper process supervision system (upstart, DJB daemontools, Apple launchd, systemd, etc) to monitor your long-running daemons and kill or restart them when needed.
By the way -- there's no need for a for loop at all: kill can be passed multiple PIDs on a single invocation, like so:
# a bit longer and bash-specific, but avoids globbing
IFS=$'\n' read -r -d '' -a pids \
< <(ps auxw | awk -v proc="$pname" -v preserve="${0##*/}" \
'$0 ~ proc && $0 !~ preserve && ! /awk/ { print $2 }' \
&& printf '\0')
kill -- "${pids[#]}"
...which could also be formulated as something like:
# setting IFS and running `set -f` necessary to make unquoted expansion safe
( IFS=$'\n'; set -f; exec kill -- \
$(ps auxw | awk -v proc="$pname" -v preserve="${0##*/}" \
'$0 ~ proc && $0 !~ preserve && ! /awk/ { print $2 }') )
grep will show , it's own process . it should be removed using grep -v option
Try like this
for i in ` ps -ef | grep "$pname" | grep -v grep | awk '{print $2}'`
do
kill -9 $i
done

How to write a shell script to find PID and Kill

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

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