0

I have two PID that are running in my linux server.

When I do followin command they give me back the PID:

pgrep -f -d ' ' /'main.py'

4752 4768

And here they are with command: ps -ef | grep main.py

ubuntu    4752  4749  3 08:07 pts/1    00:00:04 python /home/ubuntu/deploy/main.py 
ubuntu    4768  4752  5 08:07 pts/1    00:00:05 /home/ubuntu/venvs/myvirtual/bin/python /home/ubuntu/deploy/main.py

Now, I wanted to write a small shell script that when I run it, It will kill both PIDs. My script looks like this:

#!/bin/sh

PID=`pgrep -f -d ' ' /'main.py'`

if [ -z "$PID" ]; then
    echo "no pid found for main.py"
else
    echo -ne '\n' | nohup kill -KILL $PID 1>&2 &
fi

The problem is, when I run this script (./shutdown.sh) .. it first prints this:

-bash: line 56:  4727 Killed   nohup python ~/deploy/main.py > /home/ubuntu/main.log 2>&1

... then I have to press [ENTER] to get back to prompt and it then prints:

[1]+  Exit 137  . ~/venvs/myvirtual/bin/activate && nohup python ~/deploy/main.py > /home/ubuntu/main.log 2>&1  (wd: ~/deploy)
(wd now: ~)

1) How do I make it kill the PIDs and just return to prompt cleanly without me hitting a RETURN button after script is run?

2) Is there also a way to make the script fail if PID killing does not happen?

Saffik
  • 911
  • 4
  • 19
  • 45

0 Answers0