> 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)