Help sending mail through subshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help sending mail through subshell
# 8  
Old 08-01-2014
Quote:
Originally Posted by Corona688
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.

hey Corona, thanks for the detailed help.
So basically I am trying out your script however I have a question about $! 's- functionality.

does it pick from within the script the most recent shell script or does it grab from all running scripts.
I am wonderring cause the documentation on it is confusing me. and the way we schedule linux scripts we have many running simultaneously and any clarification you can give me on $! would help clear my worries about it. - I am still googling

but does it grab from the entire history linux processes running at the time or will it only grab from within the script.

I will post my final script later so you guys can look and see what I was trying in the first place.

and FYI- the reason I am seperating the DB-connections is that the system hangs on lets say the first variable assignment which is a query from the DB.

--Your solution seems to cover it so thanks, -will be testing it for a day or so to see it on my dev environment. -I will proably use once I understand the scope of $!

Thanks.

---------- Post updated at 10:50 AM ---------- Previous update was at 10:30 AM ----------

I understand $! grabs from inside script can someone confirm??
# 9  
Old 08-01-2014
Quote:
Originally Posted by mo_VERTICASQL
hey Corona, thanks for the detailed help.
So basically I am trying out your script however I have a question about $! 's- functionality.

does it pick from within the script the most recent shell script or does it grab from all running scripts.
$! is the process ID of whatever you backgrounded (i.e, ran with &) most recently inside the current shell.

So if you did ( .... ) & you would get the process ID of the subshell.

If you did sleep 1000 & you would get the process ID of sleep without any subshell involved at all.

etc.

BASH also has another meaning for "!", which relates to the shell history -- but, like aliases, this does not happen inside a shell script, only in live interactive terminal sessions.
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 08-01-2014
CORONA DUDE I LOVE YOU MAN. THE SCRIPT WORKS BEAUTIFULLY!

---------- Post updated at 11:45 AM ---------- Previous update was at 11:44 AM ----------

I will update you guys on my final deployment BUT TESTS WERE ALL GOOD Smilie
This User Gave Thanks to mo_VERTICASQL For This Post:
# 11  
Old 08-01-2014
You might want to comment out the echo inside that loop before deploying. I'm not sure where all those debug statements will end up, if anywhere. Don't want them piling up in some neglected logfile Smilie
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 08-01-2014
Will do and thanks for the heads up!
is there a any logs in specific your thinking of? i'm just curious.
what causes the pileup ? termination of the script ?

I removed the echos.
# 13  
Old 08-01-2014
Quote:
Originally Posted by mo_VERTICASQL
Will do and thanks for the heads up!
is there a any logs in specific your thinking of? i'm just curious.
I don't know, that depends on what's running it. That's why any programs you write should make as little noise as possible unless they actually have anything important to say.

If it's run by an init script, it might end up somewhere in /var/log.

If it's run by cron, any program output from that gets automatically emailed to user@hostname via the server's own MTA... or dumped in $HOME/dead.letter if none's available. (Meaning, if it is cron-ed, it might be possible to simplify that script by letting cron do the mailing for it.)

etc.

It might go nowhere at all. It's not THAT big a deal in the end. I just don't want you to end up with a slowly growing file under /var/log which will get ignored for 10 years until the partitions full... Or a confused customer wondering why they're suddenly getting 3 emails full of nonsense a day Smilie
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