Help sending mail through subshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help sending mail through subshell
# 1  
Old 07-30-2014
Help sending mail through subshell

Hey I have a shell script that is like this:
Code:
(
 echo "hi!"
##DO SOMETHING
)&(
sleep 5
##EMAIL RECIPIENTS VARs
ERECIPIENT3="email.com"
echo "Connection on status: is Down"|mail -s "Subject:" ${ERECIPIENT3}
kill -- -$$
)

This isn't working anyone know why? mail won't go out from subshell 2, however in subshell 1 I can send mail for any alerts.


Thanks!
# 2  
Old 07-30-2014
I'm not sure what you're even trying to do here.

What is the thing you're putting in background? What are you trying to do with it?

Why put the second thing in a subshell at all?

What is the & for? Are you trying to run a subshell in the background, wait for it, then kill it?
# 3  
Old 07-30-2014
Hey Corona688,



in the first subshell I have a query on a database which is waiting on a correct response, if database is slow then
the simultaneous subshell running will have commands sleep and kill -$$ and send an email to me as an alert DB is going slowly!

Thanks. Let me know what your thoughts are.

---------- Post updated at 06:24 PM ---------- Previous update was at 06:14 PM ----------

Hey a more simple code sample is:

Code:
(
echo "hi"
)&(
sleep 5
##EMAILRECIPEIENTS INITIALIZE##
ERECIPIENT3="EMAILHERE"
Echo "connection slow"|mail -s "Subject:" {ERECIPIENT3}
kill -- -$$
)

# 4  
Old 07-30-2014
Code:
(
        echo "Hi!"
        # Slow thing ) &

PID="$!"

for X in 1 2 3 4 5
do
        ps "$PID" >/dev/null 2>/dev/null || break
        sleep 1
done

if ps "$PID" >/dev/null 2>/dev/null
then
        # database query hasn't finished in 5 seconds
        kill "$PID"
fi

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-30-2014
If your OS has the /proc filesystem, you can use [ -d "/proc/$PID" ] instead of ps "$PID" >/dev/null 2>/dev/null
# 6  
Old 07-31-2014
Guys,
I will attempt your ideas today before eofday.

I however have probably not explained this properly:

I understand two subshells or 1 subshell and main command going same time.

BUT, I need to add a mail function in both.
what I am attempting is that: If the sleep 5 in subshell gets to end and the subshell its in kills -- -$$ ALL before my other main or simultaneous shell is doing some intensive/timeconsuming calculation against a SQL DATABASE -- Then Send and alert mail-s

I just need probably help getting errors directed taking everything into account in a mail command rather then how to terminate both shells.

So basically my logic is:

()
&
()
----
Shell1-
(do intensive calculations and
initialize variables for mail alert
Send mail
kill -- -$$
)
subShell2
(Sleep 5-10 depend
send mail alert if
finishes sleep saying something like (HEY WATCHOUT DATABASE SLOW, OR CRASHED!)

kill -- -$$
)

can someone elaborate how I can remove all possible errors from a mail -s function and ignore them cause in my tests yesterday I noted that thats where the hanging up is occurring, mail -s ONLY, the logic I think might be alright unless im missing something.

Again: if anyone knows how to dev/null mail properly can you add it to a simple echo blah/sleep5/sendmail/kill -- -$$ everything good while other subshell goes with (a sleep -faster time then 1st shell/ alert mail/and kill -- -$$ with other subshell

I can get one subshell to mail alerts by switching sleep times around but other wont.

Thus in the grand scheme of things,
-if my db is slow at responding -and sending an email with the variables I get from DB -and kill before other shell
-the other subshell will -sleep on side and after send mail and then kill all

Thanks Guys!
# 7  
Old 07-31-2014
Quote:
Originally Posted by mo_VERTICASQL
Guys,
I will attempt your ideas today before eofday.

I however have probably not explained this properly:

I understand two subshells or 1 subshell and main command going same time.

BUT, I need to add a mail function in both.

what I am attempting is that: If the sleep 5 in subshell gets to end and the subshell its in kills -- -$$ ALL
Again, I'm not sure what 'kill -- -$$' is even trying to do. -- is not a valid option for kill, and killing your own shell would kill everything, leaving it not able to do anything. And -(number) would tell kill it's some strange kind of signal, not a process ID.

Quote:
before my other main or simultaneous shell is doing some intensive/timeconsuming calculation against a SQL DATABASE -- Then Send and alert mail-s
This is different from our suggestions how? Wait up to 5 seconds then kill, and if it had to be killed, email?

Quote:
So basically my logic is:

()
&
()
Once again, this logic doesn't make sense. & on a line by itself is meaningless, and -- why bother making a second subshell? You already have two shells, and you never background the second one anyway.

Quote:
Shell1-
(do intensive calculations and
initialize variables for mail alert
Send mail
kill -- -$$
)

subShell2
(Sleep 5-10 depend
send mail alert if
finishes sleep saying something like (HEY WATCHOUT DATABASE SLOW, OR CRASHED!)

kill -- -$$
)
Oh, I see -- you are trying to launch two scripts simultaneously. Each will kill the other if it finishes first.

This doesn't seem a good way to solve the problem, really. That's the very definition of "race condition". Even assuming good conditions, who can say whether one will kill the other in time? Who can say whether they will die cleanly?

And there's no point! It's an over-complicated, extra-wasteful, extra-sloppy, extra-error-prone version of the exact same algorithm: "Wait up to x seconds, then kill and send email".

If you're trying to avoid some sort of problem with this convoluted method, it'd be nice to know what problem, so that we can show you how to actually avoid it.

Quote:
Again: if anyone knows how to dev/null mail properly
I don't know what you're even trying to say here.

I will make my solution more complete in the hope you will actually try it:

Code:
#!/bin/bash

(
        echo "Hi!"

        # Using 'exec' means less mess if something goes wrong,
        # since the subshell is replaced wholly by it.
        exec sleep 1000
# Less stderr noise if you use 'disown'.
# If your shell doesn't have disown, it doesn't need it.
) & disown

PID="$!"        # The PID of the background process

MAXTIME=5
X=0 ; while [ "$X" -lt "$MAXTIME" ]
do
        let X=X+1
        # If the process is done, quit immediately
        ps "$PID" 2>&1 >/dev/null || exit

        echo "$X seconds, $PID still exists" >&2
        sleep 1
done

# The query hasn't finished in 5 seconds, so kill it.
kill "$PID" >/dev/null 2>&1 || exit

ERECIPIENT3="email.com"
# echo "Connection on status: is Down"|mail -s "Subject:" "${ERECIPIENT3}" >/dev/null 2>/dev/null

sleep 1 # Wait a second for it to die

# Kill it with a more severe signal and exit if it doesn't exist
kill -QUIT "$PID" >/dev/null 2>&1 || exit
sleep 1
# Try and kill it again with an unblockable signal, quit if it doesn't exist
kill -9 "$PID" # Last resort
exit

If 'mail' is hanging, it might be a good idea to find out why.

Last edited by Corona688; 07-31-2014 at 02:21 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Problems sending mail: Difference between Mail and Mailx?

Whats the difference between mail and mailx? I'm trying to troubleshoot a problem where I can send mail from server A with this `echo $MESSAGE | mail -s "$SUBJECT" -r $FROM $RECIPIENTS` command but executing the same command from server B throws me this error (Both servers are RHEL) ... (1 Reply)
Discussion started by: RedSpyder
1 Replies

2. UNIX for Dummies Questions & Answers

Sending mail

How can i send a mail when user login in unix ENV. How can i know present use mailID?? Moved out of Contact Us Forum - Please Do Not Post Technical Questions in Non-Technical Forum(s) (1 Reply)
Discussion started by: arun508.gatike
1 Replies

3. Shell Programming and Scripting

Stop sending mail after certain number of mail

Hi guys... I am busy writing a script to notify me via an mail if my application is down. I have done that. Now I want this script to stop sending mails after five mails were sent but the script should keep on checking the application. When the application is up again that count should be... (5 Replies)
Discussion started by: Phuti
5 Replies

4. Shell Programming and Scripting

Sending mail

Hi there, How can I send the automated log file, daily at 7 am to the respective mail . Thanks in Advance, Neha (2 Replies)
Discussion started by: NehaKrish
2 Replies

5. UNIX for Dummies Questions & Answers

sending mail

i want to send an email from the unix machine to the windows machine. now windows dont have any specified folder for the mail. mail has to be sent to the email-id like abc@xyz.com unix machine itself can not directly send mail. it has to be transferred via mail server. (11 Replies)
Discussion started by: parmeet
11 Replies

6. Filesystems, Disks and Memory

Sending mail

Hi, I am want to send mails from my aix server using smtp adaptors.How to configure this? i tried with send mail command but it is failing,but what i try with my localhost(my desktop which is not using the aix server) machine i can send mails using the smtp adaptor(simply type telnet... (0 Replies)
Discussion started by: gnanadurai_it
0 Replies

7. Shell Programming and Scripting

Mail sending

Dear all, how can i send mail using mailx or mail command?do i need to configure anything for sending mail?please help me.Its urgent. the version i use is Linux TDM 2.4.21-4.EL #1 Fri Oct 3 18:13:58 EDT 2003 i686 i686 i386 GNU/Linux Thanks Regards, Pankaj (15 Replies)
Discussion started by: panknil
15 Replies

8. UNIX for Dummies Questions & Answers

Sending Mail

Please help me out i want to know how to send email from unix machine to any email-id. mail to be sent is web based mail. (1 Reply)
Discussion started by: parmeet
1 Replies

9. UNIX for Advanced & Expert Users

sending mail

How do I send an email with a subject and an attachment from a command prompt? (3 Replies)
Discussion started by: mskarica
3 Replies

10. UNIX for Dummies Questions & Answers

sending a mail to a mail client

Hi everyone! I'm trying to create a database monitoring script that reads an alert file and sends an error message if it can 'grep' a particular string. Is there a way to send this message to a mail client using SMTP? Even better, is there any place on this site that has these kinds of... (5 Replies)
Discussion started by: solaris73
5 Replies
Login or Register to Ask a Question