HELP ME! How manage kill error code?

HELP ME! How manage kill error code?

Post by Walter Brivi » Wed, 11 Nov 1998 04:00:00



I have this problem:

In a script shell an user insert a PID;
This PID is used to kill the process.

If nobody process has the PID I need to return a message like:
"Incorrect PID"

I dont know instructions to manage the error code returned by kill.
Can you help me.
I need that UNIX don't display any message.

Tank you very much.

 
 
 

HELP ME! How manage kill error code?

Post by Richard Howlet » Wed, 11 Nov 1998 04:00:00



> I have this problem:

> In a script shell an user insert a PID;
> This PID is used to kill the process.

> If nobody process has the PID I need to return a message like:
> "Incorrect PID"

> I dont know instructions to manage the error code returned by kill.
> Can you help me.
> I need that UNIX don't display any message.

Use || to test the output of kill and redirect the error message from
kill if the process doesn't exist using 2>/dev/null. eg:

kill 12345 2>/dev/null || echo Incorrect PID

--
Richard Howlett



 
 
 

HELP ME! How manage kill error code?

Post by Bruce Barnet » Thu, 12 Nov 1998 04:00:00



> I have this problem:

> In a script shell an user insert a PID;
> This PID is used to kill the process.

> If nobody process has the PID I need to return a message like:
> "Incorrect PID"

> I dont know instructions to manage the error code returned by kill.

Here is an example. Note that I use kill -0 to TEST without actually
affecting any process. Also note that when kill exits - there may be
two reasons for the error.

I just piped kill's output into a temporary file and used grep.
You may have to tweak the string used in the grep example.

I could have used if ()... instead of process && {...} || {...}
It's just a difference in style....

I could also use ps(1) and parse that, but this is faster

#!/bin/sh
# Bruce Barnett's example of testing kill - Enjoy
# remember to always delete the temporary file
# note - signal 0 occurs at the normal end-of-script
# So I don't have to delete the file at the end

TFILE="/tmp/file.$$"
trap "/bin/rm $TFILE;exit" 0 1 2 15
PID=${1?"Must specify PID of process to examine"}
kill -0 ${PID} >$TFILE 2>&1  && {
        echo yes you have permission to send ${PID} a signal

Quote:} || {

        echo no you cannot send PID  ${PID} a signal, or else the process does not exist
#       more $TFILE
        grep "permission denied" $TFILE >/dev/null && {
                echo you do not have permission to send process ${PID} a signal
        } || {
                echo the process id ${PID}  does not exist
        }

Quote:}

--
Bruce  <barnett at crd. ge. com> (speaking as myself, and not a GE employee)
 
 
 

1. HELP ME! How manage kill error code?

I have this problem:

In a script shell an user insert a PID;
This PID is used to kill the process.

If nobody process has the PID I need to return a message like:
"Incorrect PID"

I dont know instructions to manage the error code returned by kill.
Can you help me.
I need that UNIX don't display any message.

Tank you very much.

2. Query: Terrible Samba performance Linux->Win98

3. sync timing for an Apple Multiscan 20"

4. help: preserving error return codes thru function codes

5. Linux newbie + Web server

6. Help: Error in Host Manage of solstice

7. Perl and Windows or Linux

8. Help: Error in Host Manage of Solstice

9. managing KILL files in rn/xrn

10. Managing User Accounts through code

11. isdn_audio, Manage DTMF Code Detection limit

12. MANAGE SOURCE CODE CONTROL, RUN BENCHMARKS