LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-14-2014, 07:22 AM   #1
manalisharmabe
Member
 
Registered: Dec 2010
Posts: 242

Rep: Reputation: 1
How to write this SIMPLE SCRIPT based on output to send mail ?


Hi Guys,

I am not Good at scripting.

I need to write a script such that if output of command shows the particular word in output then send mail to abc@compay.com

Code:
-bash-3.2$ ps -ef | grep bpbkar
    root  6040     1   0 13:05:19 ?           0:00 bpbkar -r 2678400 -ru root -dt 47395 -to 0 -clnt server1-backup-class server1
    root  6707     1   0 13:05:22 ?           0:00 bpbkar -r 2678400 -ru root -dt 47401 -to 0 -clnt server1-backup-class server1
    root  6302     1   2 13:05:20 ?           0:07 bpbkar -r 2678400 -ru root -dt 47403 -to 0 -clnt server1-backup-class server1
    root  5869     1   1 13:05:19 ?           0:05 bpbkar -r 2678400 -ru root -dt 47393 -to 0 -clnt server1-backup-class server1
    root  6493     1   2 13:05:21 ?           0:08 bpbkar -r 2678400 -ru root -dt 47402 -to 0 -clnt server1-backup-class server1
user1  9186 19295   0 13:05:33 pts/22      0:00 grep bpbkar
-bash-3.2$
in above output if the word server1-backup appears in the out put then it should send mail to abc@company.com that the "Server1 backup still running!"

This is Solaris 10 OS.

Please advise.

Thanks,
Manali.

Last edited by manalisharmabe; 04-14-2014 at 07:29 AM.
 
Old 04-14-2014, 07:37 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Post what you've got so far and what problem you have.
 
1 members found this post helpful.
Old 04-14-2014, 07:58 AM   #3
manalisharmabe
Member
 
Registered: Dec 2010
Posts: 242

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by chrism01 View Post
Post what you've got so far and what problem you have.
I need to make script for following thing:-

if "ps -ef | grep bpbkar" shows server1-backup in output then send mail to abc@company.com

Code:
/usr/bin/mailx -r abc@company.com  -s "Server1 backup still runing!"
I need to send mail like above.

Thanks a lot for replying

Last edited by manalisharmabe; 04-14-2014 at 08:22 AM.
 
Old 04-14-2014, 08:57 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
use pgrep -f <pattern> && mailx ...
 
1 members found this post helpful.
Old 04-15-2014, 01:21 AM   #5
lordadamson
Member
 
Registered: Nov 2013
Location: Egypt
Distribution: Fedora
Posts: 32

Rep: Reputation: Disabled
not sure if i understand you but try this

Code:
ps -ef | grep -q server1-backup && /usr/bin/mailx -r abc@company.com -s "Server1 backup still runing!"
 
1 members found this post helpful.
Old 04-15-2014, 01:37 AM   #6
lordadamson
Member
 
Registered: Nov 2013
Location: Egypt
Distribution: Fedora
Posts: 32

Rep: Reputation: Disabled
well that line I just wrote won't work, it will always be true

but I believe pan64's will work just fine
 
1 members found this post helpful.
Old 04-15-2014, 02:11 AM   #7
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 207

Rep: Reputation: 39
Something like this?

Code:
      1 #!/bin/bash
      2 
      3 if ps -ef | grep -q $1 ; then
      4         mailx -r abc@company.com -s "$1 is running."
      5 else
      6         mailx -r abc@company.com -s "$1 is not running, attention is required."
      7 fi
 
Old 04-15-2014, 04:48 AM   #8
manalisharmabe
Member
 
Registered: Dec 2010
Posts: 242

Original Poster
Rep: Reputation: 1
or this?

Code:
cat check_backup

#!/bin/bash
# Script to check if Server1 backups still running.

if [ `ps -ef | grep bpbkar | nawk '{print gsub(/server1-backup/," ",$0)}'` -eq 1 ]
then
mailx -s "Server1 backup still running!" -r abc@company.com
fi
Thanks for replies.
 
Old 04-15-2014, 04:57 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Code:
if [ `ps -ef | grep bpbkar | nawk '{print gsub(/server1-backup/," ",$0)}'` -eq 1 ]
as I told a pgrep would be much simpler. I'm not really sure this if [ ps|grep|nawk ] will work in every possible case.
 
1 members found this post helpful.
Old 04-15-2014, 05:42 AM   #10
manalisharmabe
Member
 
Registered: Dec 2010
Posts: 242

Original Poster
Rep: Reputation: 1
Thanks for reply.

Please recorrect the script then, I am not good at this. I got it from google. :-)
 
Old 04-15-2014, 05:47 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
you would need to practice a bit, that would not be that hard at all:
Code:
pgrep -f <pattern> && mailx ...
the mailx command is already ok I think, you only need to find out the pattern.
pattern can be something like: 'root.*bpbkar.*server1'
 
Old 04-16-2014, 06:55 AM   #12
manalisharmabe
Member
 
Registered: Dec 2010
Posts: 242

Original Poster
Rep: Reputation: 1
Code:
The flags you gave are used only when sending mail.
Usage: mailx -eiIUdFntBNHvV~ -T FILE -u USER -h hops -r address
                -s SUBJECT -f FILE users
I got above Error for below script.

Code:
]#!/bin/bash

# Script to check if Server1 backups still running.

if [ `ps -ef | grep bpbkar | nawk '{print gsub(/server1-backup/," ",$0)}'` -eq 1 ]

then

mailx -s "Server1 backup still running!" -r abc@company.com

else

mailx -s "Server1 backup Completed!" -r abc@company.com

fi
however below script does not send any error:-

Code:
cat check_backup

#!/bin/bash
# Script to check if Server1 backups still running.

if [ `ps -ef | grep bpbkar | nawk '{print gsub(/server1-backup/," ",$0)}'` -eq 1 ]
then
mailx -s "Server1 backup still running!" -r abc@company.com
fi

Last edited by manalisharmabe; 04-16-2014 at 06:56 AM.
 
Old 04-16-2014, 07:18 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You've got an extra character at the start of the first one
Code:
]#!/bin/bash
I also recommend using the 'set' debug cmd as the 2nd line thus
Code:
#!/bin/bash
set -xv
 
Old 04-16-2014, 07:23 AM   #14
manalisharmabe
Member
 
Registered: Dec 2010
Posts: 242

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Tadaen View Post
Something like this?

Code:
      1 #!/bin/bash
      2 
      3 if ps -ef | grep -q $1 ; then
      4         mailx -r abc@company.com -s "$1 is running."
      5 else
      6         mailx -r abc@company.com -s "$1 is not running, attention is required."
      7 fi
I used this script I got below Error:-

Code:
-bash-3.2$ cat server1-weekly_2nd
#!/bin/bash
# Script to check if SERVER1 Weekly Backups still running at 5 A.M. CET.
if ps -ef | grep server1-backup ; then
mailx -r  abc@company.com -s "SERVER1 Weekly Backup still running!"
else
mailx -r abc@company.com -s "SERVER1 Weekly Backup COMPLETED!"
fi

-bash-3.2$
I got below error:-

Code:
-bash-3.2$ sudo ./server1-weekly_2nd
    root 18537     1   1 13:06:45 ?           0:55 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
    root 18591     1   1 13:06:51 ?           0:56 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
    root 18521     1   1 13:06:44 ?           0:52 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
    root 18555     1   1 13:06:49 ?           0:57 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
    root 18572     1   1 13:06:50 ?           0:56 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
    root 18529     1   1 13:06:44 ?           0:49 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
    root 18507     1   1 13:06:42 ?           1:13 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
    root 21086 21084   0 13:16:35 pts/25      0:00 grep server1-backup
The flags you gave are used only when sending mail.
Usage: mailx -eiIUdFntBNHvV~ -T FILE -u USER -h hops -r address
                -s SUBJECT -f FILE users
Please advise. Thanks
 
Old 04-16-2014, 07:28 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
you ought read the man page of mailx about its flags and usage. the message you got means the command you tried to execute is not correct.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to send mail when the mail queue is more than 100 Arun Kurian Linux - Server 2 11-06-2013 11:53 AM
Send input from one script to another menu based script. simonedgcumbe Programming 9 03-24-2010 10:22 PM
How can I write a script to send general mail to my users at 9AM everyday in Linux 4 mostafaashish Linux - General 3 03-03-2010 07:19 AM
Using 'mail' in a script to send mail on an alternate port? tim914 Linux - Software 4 01-16-2009 04:23 PM
CGI Script runs to send mail, but mail is never sent robertwo Linux - Newbie 2 06-10-2004 10:57 AM


All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration