0

I have a script to Monitor servers in Production , and I want to receive the result of this script execution every 2 hours by mail , how can I do this ? someone has an example ? this is my Script:

    #!/bin/sh
CheckPorts=`cd /DATA/scripts && sh checkPorts.sh`

CPU=`iostat -ch`

FreeMemory=`free -mh`

DISK_Space_DATA=`df -h /DATA`

DISK_Space_ROOT=`df -h /root`

Kafka=`ps -aux | grep kafka`

Spark=`ps -aux | grep spark`
# -CheckPorts:
echo -e "\e[31;43m***** Firewall Authorization *****\e[0m"

echo "${CheckPorts}"

echo ""
# -CPU:
echo -e "\e[31;43m***** CPU INFORMATION *****\e[0m"

echo "${CPU}"

echo ""
# -Free and used memory in the system:
echo -e "\e[31;43m ***** FREE AND USED MEMORY *****\e[0m"

echo "${FreeMemory}"

echo ""
# -File system disk space usage:
echo -e "\e[31;43m***** FILE SYSTEM /DATA DISK SPACE USAGE *****\e[0m"

echo "${DISK_Space_DATA}"

echo ""
# -File system disk space usage:
echo -e "\e[31;43m***** FILE SYSTEM /root DISK SPACE USAGE *****\e[0m"

echo "${DISK_Space_ROOT}"

echo ""
# -Spark Process Status:
echo -e "\e[31;43m***** Spark Process Status *****\e[0m"

echo "${Spark}"

echo ""
# -Kafka PROCESSES Status:
echo -e "\e[31;43m***** Kafka Processes Status *****\e[0m"

echo "${Kafka}"

echo ""

echo "Done" 
4
  • 1
    If local mail delivery is set up on the machine, the output of cron jobs are mailed to the owner of the job. The mail is sent to the owner, or to the address specified in $MAILTO in the crontab. See man 5 crontab.
    – Kusalananda
    Aug 8 '19 at 10:16
  • I did'nt get your point , I'm new in Linux so what you said is not clear for me Aug 8 '19 at 10:26
  • 3
    Have you already set up a cron job to run this script every two hours? It is not clear from the question whether the primary issue is getting the output mailed, or having the script execute every two hours (which would actually mail the output automatically).
    – Kusalananda
    Aug 8 '19 at 10:27
  • I did a cron job , but in the log cron.log I don't find the result , there is just the path and permission denied , what does it means ? Aug 9 '19 at 7:43
1

Suppose your script is called monitor.sh. There are many commands you can use on linux to send the email.

For example with mail command:

mail -s "Monitor Report" admin@example.com < <( bash monitor.sh )

With sendmail:

echo "Subject: Monitor Report" | sendmail -v admin@example.com < <( bash monitor.sh )

With ssmtp:

ssmtp admin@example.com < <( echo "Subject: Monitor" ; bash monitor.sh )

With curl and gmail:

bash monitor.sh > report.txt
curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
  --mail-from 'developer@gmail.com' --mail-rcpt 'admin@example.com' \
  --upload-file report.txt --user 'developer@gmail.com:your-accout-password'

If you want this automatically every 2 hours, put the command on cron (see man crontab).

Chances are, your server already has one of the commands above properly configured to send email (sendmail, mail, ssmtp), if not, take a look how to install and configure those.


References:

https://blog.edmdesigner.com/send-email-from-linux-command-line/

https://tecadmin.net/ways-to-send-email-from-linux-command-line/

7
  • I have used the method with sendmail and I get /usr/sbin/sendmail: No such file or directory seems it needs something to be installed but I don't want to install something in the server because it's a big procedure to do it Aug 8 '19 at 12:16
  • @AbdelAzizESSAADI Well, you need a program installed to send the email, there is no magic around that, see if your server has either mail, mailx, ssmtp, curl, wget, telnet.. Check the first link I pasted, it shows how to run many of these commands. If your server doesn't have any of those, you will need to have one of those installed.
    – Fabiano
    Aug 8 '19 at 12:25
  • okey thank you so much Aug 8 '19 at 12:28
  • the server as mail but it's not working when I put the command !! I don't receive anything */2 * * * * /DATA/scripts/Daily_Script.sh &> /DATA/scripts/cron.log && mail -s "APP2_Monitoring_Log" mail@example.ma </DATA/scripts/cron.log Aug 11 '19 at 14:44
  • Any other help ?? Aug 15 '19 at 12:33
0

May be this will help you to get the expected result

echo "Hourly report : \n $(cat \$file.log)" | mailx -s "Subject : add_your_subject" example@gmail.com

You can even add -a to above mailx command to attach the log generated as an attachment

make sure if you want to add attachment .. generate the log with date+time like below

ABC_12_09_2020_1_56_34.log  

you can write some script that will pick latest file as an when the new log is generated and your mailx command while running should pick the latest log as an attachment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.