0

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.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
jurh
  • 420
  • 1
  • 4
  • 17

2 Answers2

1

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.

kalou.net
  • 446
  • 1
  • 4
  • 16
0

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!

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49