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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
04-14-2014, 06:22 AM
#1
Member
Registered: Dec 2010
Posts: 242
Rep:
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 06:29 AM .
04-14-2014, 06:37 AM
#2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359
Post what you've got so far and what problem you have.
1 members found this post helpful.
04-14-2014, 06:58 AM
#3
Member
Registered: Dec 2010
Posts: 242
Original Poster
Rep:
Quote:
Originally Posted by
chrism01
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 07:22 AM .
04-14-2014, 07:57 AM
#4
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838
use pgrep -f <pattern> && mailx ...
1 members found this post helpful.
04-15-2014, 12:21 AM
#5
Member
Registered: Nov 2013
Location: Egypt
Distribution: Xubuntu
Posts: 33
Rep:
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.
04-15-2014, 12:37 AM
#6
Member
Registered: Nov 2013
Location: Egypt
Distribution: Xubuntu
Posts: 33
Rep:
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.
04-15-2014, 01:11 AM
#7
Member
Registered: Sep 2005
Distribution: Arch
Posts: 210
Rep:
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
04-15-2014, 03:48 AM
#8
Member
Registered: Dec 2010
Posts: 242
Original Poster
Rep:
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.
04-15-2014, 03:57 AM
#9
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838
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.
04-15-2014, 04:42 AM
#10
Member
Registered: Dec 2010
Posts: 242
Original Poster
Rep:
Thanks for reply.
Please recorrect the script then, I am not good at this. I got it from google. :-)
04-15-2014, 04:47 AM
#11
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838
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'
04-16-2014, 05:55 AM
#12
Member
Registered: Dec 2010
Posts: 242
Original Poster
Rep:
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 05:56 AM .
04-16-2014, 06:18 AM
#13
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359
You've got an extra character at the start of the first one
I also recommend using the 'set' debug cmd as the 2nd line thus
Code:
#!/bin/bash
set -xv
04-16-2014, 06:23 AM
#14
Member
Registered: Dec 2010
Posts: 242
Original Poster
Rep:
Quote:
Originally Posted by
Tadaen
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
04-16-2014, 06:28 AM
#15
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838
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.
All times are GMT -5. The time now is 10:48 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News