sending email as background process

Hi All,

Solaris
Bash v3x

I have a script that accepts an error code, and if the error code is not 0 then an email is sent using mailx to details the error.

I want to be able to implement the functiuonlity whereby i can send the email in a background process so the script can continue with other logic
and if the email has not completed say within 30 seconds then to abort

does anyone have a code snippet that I could use/ammend?

something like...

#!/bin/bash

<< normal code>>

<<start background process here>>
echo �message� | mailx -s �<Subject>� <recipient mail address>
if not responsing or email not sent within 30 seconds then abort with error message

<< continue normal code here>>

any ideas much appreciated..

Kind Regards
Satnam

Hi.

One way would be to write a small function:

MAX_TIME=30

function CheckAndKill {
  sleep $MAX_TIME
  [ "$(ps | awk '$1 == '$1)" ] && echo "Killing process $1" && kill -9 $1
}
....
....
echo �message� | mailx -s �<Subject>� <recipient mail address>
CheckAndKill $! &

Hi Scott

Thanks for the reply..looks intersting and am trying it.. any ideas how I would ammend it to execute the mailing to a background process?

Kind regards
Satnam

---------- Post updated at 06:26 PM ---------- Previous update was at 06:06 PM ----------

Hi Scott,

Just 2 questions:

  1. The commad CheckAndKill $! & ... is the & a 2nd parameter, if so not sure what it represents?
    Regards
    Satnam

Hi.

The & will run the function in the background.

You can add a & to the end of your mail command too (sorry I forgot to add that in my original reply).

Cheers.