9

I have an application server which we start by running this below command and as soon as we run the below commnad, it will start my application server

/opt/data/bin/test_start_stop.sh start

And to stop my application server, we do it like this -

/opt/data/bin/test_start_stop.sh stop

So as soon as my application server is running and if I do ps aux, this is what I get -

user@machineA:/home/user$ ps aux | grep gld_http
user 20426  0.0  0.0   8114   912 pts/4    S+   13:33   0:00 grep gld_http
user 40509  0.1  0.9 3878120 1263552 ?     Sl   Sep22   1:53 /opt/data/bin/gld_http

Now I am trying to make a shell script in which I need to stop my application server and then I will check whether my process is running after stopping the server or not, if my process is still running, then I need to do kill -9 of my application pid. And I am not sure how to get the pid of my process in the shell script and then kill it.

Below is my shell script which I have as of now which will shut down my app server and then check whether my process is still running or not and if by any chance it is still running, then I need to get the pid of my process and do kill -9 on that pid.

#!/bin/bash

/opt/data/bin/test_start_stop.sh stop

if ps aux | grep -v "grep" | grep "gld_http"
then
    echo "Server is still running"
    # now get the pid of my gld_http process and invoke kill -9 on it
else
   echo "Server is stopped"
fi

How can I get the pid of my process and do kill -9 on it in my above shell script?

UPDATE:-

So my final script will look like this -

#!/bin/bash

/opt/data/bin/test_start_stop.sh stop

if ps aux | grep -v "grep" | grep "gld_http"
then
    echo "Server is still running"

    # Getting the PID of the process
    PID=`pgrep gld_http`

    # Number of seconds to wait before using "kill -9"
    WAIT_SECONDS=10

    # Counter to keep count of how many seconds have passed
    count=0

    while kill $PID > /dev/null
    do
        # Wait for one second
        sleep 1
        # Increment the second counter
        ((count++))

        # Has the process been killed? If so, exit the loop.
        if ! ps -p $PID > /dev/null ; then
            break
        fi

        # Have we exceeded $WAIT_SECONDS? If so, kill the process with "kill -9"
        # and exit the loop
        if [ $count -gt $WAIT_SECONDS ]; then
            kill -9 $PID
            break
        fi
    done
    echo "Process has been killed after $count seconds."    
else
   echo "Server is stopped"
fi
david
  • 2,187
  • 7
  • 25
  • 31

3 Answers3

12

First of all; "kill -9" should be a last resort.

You can use the kill command to send different signals to a process. The default signal sent by kill is 15 (called SIGTERM). Processes usually catch this signal, clean up and then exit. When you send the SIGKILL signal (-9) a process has no choice but to exit immediately. This may cause corrupted files and leave state files and open sockets that might cause problems when you restart the process. The -9 signal can not be ignored by a process.

This is how I would do it:

#!/bin/bash

# Getting the PID of the process
PID=`pgrep gld_http`

# Number of seconds to wait before using "kill -9"
WAIT_SECONDS=10

# Counter to keep count of how many seconds have passed
count=0

while kill $PID > /dev/null
do
    # Wait for one second
    sleep 1
    # Increment the second counter
    ((count++))

    # Has the process been killed? If so, exit the loop.
    if ! ps -p $PID > /dev/null ; then
        break
    fi

    # Have we exceeded $WAIT_SECONDS? If so, kill the process with "kill -9"
    # and exit the loop
    if [ $count -gt $WAIT_SECONDS ]; then
        kill -9 $PID
        break
    fi
done
echo "Process has been killed after $count seconds."
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
arnefm
  • 3,172
  • Thanks for suggestion. Can you add little bit explanation in your example for me to understand as I am still a beginner in linux world. I got few things but some of the things I was not able to follow. – david Sep 23 '14 at 22:57
  • Updated my answer! – arnefm Sep 23 '14 at 23:01
  • Thanks makes sense now. Is there any difference between kill pid vs kill -9 pid and why you are saying it has to be a last resort. – david Sep 23 '14 at 23:08
  • Updated again. You should also see this: http://unix.stackexchange.com/questions/8916/when-should-i-not-kill-9-a-process – arnefm Sep 23 '14 at 23:19
  • Awesome. I learned pretty useful information today. Appreciated your help. I have updated my question which has the final script which includes your suggestion. Can you take a look to see I got everything correct? – david Sep 23 '14 at 23:23
  • I have not tested your complete version but it looks fine to me. Personally I would use if pgrep gld_http > /dev/null in place of your if ps aux | grep -v "grep" | grep "gld_http" but that is just a detail (personal preference). If it works it works!

    You might want to add sleep 1 or even sleep 0.5 after /opt/data/bin/test_start_stop.sh stop to give the process a little time to shut down before killing it.

    – arnefm Sep 24 '14 at 00:02
3

pidof gld_http should work if it is installed on your system.

man pidofsays:

Pidof  finds  the process id’s (pids) of the named programs. It prints those id’s on the standard output.

EDIT:

For your application you can use command substitution:

kill -9 $(pidof gld_http)

As @arnefm mentioned, kill -9 should be used as a last resort.

  • Thanks Timothy. Yes I have pidof installed it looks like. So In general I need to do it like this right? kill -9 pidof gld_http? – david Sep 23 '14 at 22:39
  • And this is the error I got - kill: pidof: arguments must be process or job IDs Anything wrong I am doing? – david Sep 23 '14 at 22:43
0

You would also check /proc/1234 (known pid) if process is running. lot of application save pid under /var/log for the reference to make sure it is the same process in case of duplicate name.

You can save pid by redirecting the output of echo $! > /var/log/gld_http.pid in your script right after you call for the program.

Raza
  • 4,109