How to write this script:- check output word and send a 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

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

ps -ef | grep bpbkar | grep server1-backup > output.log
 
uuencode filename output.log| mailx -s " SUBJECT" -r abc@company.com 
 

try this

1 Like

let me check

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

if you want condition

---------- Post updated at 07:05 AM ---------- Previous update was at 07:03 AM ----------

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

i thought you want to send output as attachment,if you want to send a output then useabove statement

1 Like

Wow! Good info . Let me check.
Thanks a lot.

I guess this how my script should look like

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

Hope this will work on Solaris 10 too.

but honestly I cant understand what does this mean --> nawk '{print gsub(/server1-backup/," ",$0)}'` -eq 1 ]
What is gsub ?

Thanks

gsub replaces all of the longest, leftmost, nonoverlapping matching substrings it can find. The `g' in gsub stands for "global," which means replace everywhere,

So if in your output "server1-backup" word appears it will replace by 1..

so if it equal to 1 means that string is there.

then send mail..

1 Like

Wow! Good explination , I am not good at scrpting but I save all the scripts whcih i get along with it explinations, that how I learn scripting. :slight_smile:

Good.Enjoy:)

Thanks a lot.

But I have some queries

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 ]
mailx -s "Server1 backup still running!" -r abc@company.com
then
mailx -s "Server1 backup Completed!" -r abc@company.com
fi

How to write in above format?
if yes then send running else send completed.

Thanks.

yes.Above code looks fine to me...

---------- Post updated at 04:53 AM ---------- Previous update was at 04:52 AM ----------

Sorry misread it.Use below 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
1 Like

Let me try! :slight_smile:

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

Please check with following.As i am not suppose to use mailx command from my server i havent tested it...

#!/bin/bash
# Script to check if Server1 backups still running.
if [ `ps -ef | grep bpbkar | nawk '{print gsub(/server1-backup/," ",$0)}'` -eq 1 ]
then
echo "Server1 backup still running" | mailx -s "Server1 backup still running!" abc@company.com
else
echo "Server1 backup Completed!" | mailx -s "Server1 backup Completed!" abc@company.com
fi
  

I ALSO used this script I got below Error:-

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

-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

have u use below 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
echo "Server1 backup still running" | mailx -s "Server1 backup still running!" abc@company.com
else
echo "Server1 backup Completed!" | mailx -s "Server1 backup Completed!" abc@company.com
fi
1 Like

5 minutes please, I am checking it now.

---------- Post updated at 05:06 PM ---------- Previous update was at 04:58 PM ----------

./server1_weekly_3rd: line 3: [: too many arguments

Got this Error

----------------------
Also see below:-

is pgrep would be much simpler? I'm not really sure this if [ ps|grep|nawk ] will work in every possible case.
__________________

can you show your shell script server1_weekly_3rd

1 Like
-bash-3.2$ cat server1_weekly_3rd
#!/bin/bash
# Script to check if Server1 backups still running.
if [ `ps -ef | grep bpbkar | nawk '{print gsub(/server1-backup/," ",$0)}'` -eq 1 ]
then
echo "Server1 Weekly backup still running" | mailx -s "Server1 Weekly backup still running!" abc@company.com
else
echo "Server1 Weekly backup Completed!" | mailx -s "Server1 Weekly backup Completed!" abc@company.com
fi
 
-bash-3.2$

Please see.

-bash-3.2$ ps -ef | grep bpbkar
root 27447 1 1 13:34:52 ? 2:01 bpbkar -r 2678400 -ru root -dt 0 -to 0 -clnt server1-backup -class server1_
user1 11780 5521 0 13:45:30 pts/25 0:00 grep bpbkar
-bash-3.2$

---------- Post updated at 05:20 PM ---------- Previous update was at 05:15 PM ----------

Update:-

However I have received "Server1 Weekly backup Completed!" message through mail now twice looks like some came from previous execution too.

I am waiting for your valuable reply.

Hi Manali

ps -ef | grep bpbkar | nawk '{print gsub(/server1-backup/," ",$0)}'

is working perfectly fine on my system.

Only diff is i am using

KSH

& you are

BASH

Please check how

gsub

awk function works with bash.

Also check mails systax for bash.

1 Like

Why can't you create wrapper on your process "bpbkar"
something like this

#!/bin/bash
bpbkar -r 2678400 -ru root -dt 47395 -to 0 -clnt server1-backup-class server1 &
pid=$!
echo "Server1 backup started" | mailx -s "Server1 backup started!" abc@company.com
wait $pid
echo "Server1 backup Completed!" | mailx -s "Server1 backup Completed!" abc@company.com
1 Like