Kill -9 within Bash script kicks out usage info


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill -9 within Bash script kicks out usage info
# 1  
Old 07-09-2009
Kill -9 within Bash script kicks out usage info

I have a start|stop|restart script for a custom app we have. After it tries to stop our process the correct way, it checks to see if it's gone, if not it tries to kill it, if that doesn't work kill -9.

If I run kill -9 on the PID from the command line it kills it and all is well. If I have the same command within my script it kills it properly, but it also kicks out some usage info. I know I could redirect stdout/stderr for the kill command within the script but I'd rather not if possible.

Here's the code snip:

Code:
  # Check running processes against our list of targets.  If any of
  # them are still running try to kill.
  stoptargetcount=${#stoptargets[@]}
  for ((i=0;i<$stoptargetcount;i++)); do
    found=0
    for ((j=0;j<$resincount;j++)); do
      if [ "${stoptargets[$i]}" == "${instancename[$j]}" ]; then
        found=1
        killjavapid=${javapid[$j]}
        killwrapperpid=`ps alx | grep $killjavapid | grep -v "grep" | awk '{ print $4 }'`
      fi
    done
    if [ $found -eq 1 ]; then
      if [ $pass -eq 1 ]; then
        echo "${stoptargets[$i]} appears to be hung.  Attempting kill..."
        sudo kill $killwrapperpid
        sudo kill $killjavapid
        waiting 5
      elif [ $pass -eq 2 ]; then
        echo "${stoptargets[$i]} is still hung.  Attempting kill -9..."
        echo "WARNING - This may result in zombied child processes"
        sudo kill -9 $killwrapperpid
        sudo kill -9 $killpid
        waiting 5
      else
        echo "ERROR - There appears to be something wrong with the script...exiting"
        exit 1
      fi
      recheck=1
    fi
  done

and the output...

Code:
Checking instance selection...
inst04 found
inst02 found
Stopping instances...
Waiting 5 seconds...
inst04 appears to be hung.  Attempting kill...
Waiting 5 seconds...
inst02 appears to be hung.  Attempting kill...
Waiting 5 seconds...
inst04 is still hung.  Attempting kill -9...
WARNING - This may result in zombied child processes
usage: kill [ -s signal | -p ] [ -a ] pid ...
       kill -l [ signal ]
Waiting 5 seconds...
inst02 is still hung.  Attempting kill -9...
WARNING - This may result in zombied child processes
usage: kill [ -s signal | -p ] [ -a ] pid ...
       kill -l [ signal ]
Waiting 5 seconds...

# 2  
Old 07-09-2009
There's a bash builtin 'kill' which is different from the '/usr/bin/kill'.
Also when you do 'sudo' it most likely defaults to '/bin/sh' which has no builtin 'kill'.
The 'signatures' for '/usr/bin/kill' and bash's builtin 'kill' are different.
'man bash':
Code:
     kill [-s sigspec | -n signum | -sigspec] [pid | jobspec] ...
     kill -l [sigspec | exit_status]
          Send the signal named  by  sigspec  or  signum  to  the
          processes named by pid or jobspec.  sigspec is either a
          signal name such as SIGKILL or a signal number;  signum
          is  a  signal number.  If sigspec is a signal name, the
          name may be given with or without the SIG  prefix.   If
          sigspec  is  not  present, then SIGTERM is assumed.  An
          argument of -l lists the signal names.   If  any  argu-
          ments  are  supplied when -l is given, the names of the
          signals corresponding to the arguments are listed,  and
          the return status is 0.  The exit_status argument to -l
          is a number specifying either a signal  number  or  the
          exit  status of a process terminated by a signal.  kill
          returns true if at least one  signal  was  successfully
          sent,  or false if an error occurs or an invalid option
          is encountered.


Last edited by vgersh99; 07-09-2009 at 04:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Kill a program from bash

to kill a program in bash, for instance 'mousepad' I use kill $(pidof mousepad); or pkill mousepad But it only works if we use another bash window; If it is started from the same bash, that does not work: #mousepad;kill $(pidof mousepad); In this case, it looks like mousepad hangs... (7 Replies)
Discussion started by: arpagon
7 Replies

2. Shell Programming and Scripting

Bash Info Display

I have written the following bash function prArgv Suppose the calling sequence is as follows prArgv VAL1 VAL2 DESC VAL3 VAL4 v2d1 s4 p15 The call will look at the tag k1v2, add the numbers together, in this case 2+1=3 This means that the function will look at the first 3 user arguments... (1 Reply)
Discussion started by: kristinu
1 Replies

3. SCO

Console GUI kicks me after 30 seconds. Licensing issue?

I have two SCO servers. On one I can logon to the gui via console with no problem. However, on the other I can logon to the gui but after a few seconds it kicks me back out to the prompt. I asked previous admin about this and he seems to think its a license issue? (7 Replies)
Discussion started by: herot
7 Replies

4. UNIX for Dummies Questions & Answers

usage of kill

i'm writing a code that passes the shells process id and the time set till the pid is killed. the argument from the terminal looks like this: ./timeout $$ 4 now $$ specifies the shell pid, how can insert that to kill(pid,signum) as an parameter? (0 Replies)
Discussion started by: l flipboi l
0 Replies

5. Shell Programming and Scripting

Bash script show Kill system output

Hi we are calling kill -9 $pid command from bash script it gives below output, but we need to hide the output. i tried /dev/null but ni luck. is there any alternate way to schive this. ../kill_scr.sh: line 42: 1891 Killed /tmp/anr_rest_mul_wc.sh Soalris 10. ... (2 Replies)
Discussion started by: sachinbutala
2 Replies

6. Shell Programming and Scripting

Run a bash script, display on the screen and save all information in a file including error info

Hi all, How to: Run a bash script, display on the screen and save all information in a file including error information. For example: I have a bash script called test.sh now I want to run the test.sh and display the output on the screen and save the output including error info to a file. ... (1 Reply)
Discussion started by: Damon sine
1 Replies

7. Shell Programming and Scripting

Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of... (3 Replies)
Discussion started by: graysky
3 Replies

8. UNIX for Dummies Questions & Answers

kill/pkill process by CMD info.

I have a process that I'd like to kill. Doing a "ps -fu myusername" gives me: UID PID PPID C STIME TTY TIME CMD myusername 5443 1 0 10:05 ? 00:00:00 /bin/sh /some/path/crap.sh -s /yet/another/path/parentProcess myusername 5593 5443 0 ... (2 Replies)
Discussion started by: mrwatkin
2 Replies

9. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

10. UNIX for Dummies Questions & Answers

diskspace usage info

Anybody can help me? I need to know a specified folder diskspace usage, that's how much bytes that folder and its files/folders below spend on my storage. Thanks (2 Replies)
Discussion started by: nusajunus
2 Replies
Login or Register to Ask a Question