My bash script won't execute commands after kill command - linux

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!

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

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

Why part of the script cannot execute in the crontab

I have a script stopping the application and zipping some files:
/home/myname/project/stopWithZip.sh
With the properties below:
-rwxrwxr-x. 1 myname myname778 Jun 25 13:48 stopWithZip.sh
Here is the content of the script:
ps -ef | grep project | grep -v grep | awk '{print $2}' |xargs kill -15
month=`date +%m`
year=`date +%Y`
fixLogs=~/project/log/fix/$year$month/*.log.*
errorLogs=~/project/log/error/$year$month/log.*
for log in $fixLogs
do
if [ ! -f "$log.gz" ];
then
gzip $log
echo "Archived:"$log
else
echo "skipping" $log
fi
done
echo "Archived fix log files done"
for log in $errorLogs
do
if [ ! -f "$log.gz" ]; then
gzip $log
echo "Archived:"$log
else
echo "skipping" $log
fi
done
echo "Archived errorlog files done"
The problem is except this ps -ef | grep project | grep -v grep | awk '{print $2}' |xargs kill -15 command, other gzip commands are not executed. I totally don't understand why.
I cannot see any compression of the logs in the directory.
BTW, when I execute the stopWithZip.sh explicitly in command line, it works perfectly fine.
In crontab:
00 05 * * 2-6 /home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1 (NOT work)
In command line:
/home/myname/project>./stopWithZip.sh (work)
Please help
The script fails when run under cron because your script is invoked with project in its path, so the kill pipeline kills the script too.
You could prove (or disprove) this by adding some tracing. Log the output of ps and of awk to log files:
ps -ef |
tee /tmp/ps.log.$$ |
grep project |
grep -v grep |
awk '{print $2}' |
tee /tmp/awk.log.$$ |
xargs kill -15
Review the logs and see that your script is one of the processes being killed.
The crontab entry contains:
/home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1
When ps lists that, it contains 'project' and does not contain 'grep' so the kill in the script kills the script itself.
When you run it from the command line (using a conventional '$' as the prompt), you run:
$ ./stopWithZip.sh
and when ps lists that, it does not contain 'project' so it is not killed.
If you ran:
$ /home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1
from the command line, like you do with cron (crontab), you would find it fails.

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