Killing the Wrong Background Process

BASH on Solaris

Hi All,

I posted a problem whereby I was looking to Kill a background (calling Send)mail) process after a certain time had elapsed.

A User Scottn very kindly provided a useful function to do this as below

CheckAndKill()
{

 sleep "$EMAIL\_TIMEOUT\_THEN_KILL"
 [ "$\(ps | awk '$1 == '$1\)" ] && echo "Killing process $1" && kill -9 "$1"

}

Howvere I am calling this in the wrong way as below:

send_email "Warning Notification" $warning_msg" | tee -a "$LOG_PATH"/email_output.lst &
CheckAndKill $! &

When I check the shell output I can see that I am killing the wrong process ie I am killing the "tee" not the actual send_email function.

The $! passed into CheckAndKill() is clearly picking on the process id of the tee and not the send_email function
Any ideas how to correctly kill the send_email function?

Kind Regards
Satnam

You could modify what you have to use pkill instead of kill so that you can kill a process by name rather than pid and pass it certain attributes (like user id or terminal id) to restrict which process it kills.

----------------------------------------------

If you continue to call it the way you are now, if you extract the PPID when calling ps, you can pkill with process name and PPID.

Is there no way to make use of the existing ode and derive the correct process id before I pass it into ChekandKill()?

Kind regards
S