Sponsored Content
Top Forums Shell Programming and Scripting Help sending mail through subshell Post 302911400 by Corona688 on Thursday 31st of July 2014 01:07:40 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 03:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy