MIX
------------
How to List Jobs in Crontab
# crontab -l
How to Add New Job in Crontab
# crontab -e
2 LAN getwatway Failur
Webmin - Edit Cron Job
Execute cron job as: root
Command: /usr/bin/perl /scripts/gwping.sh
Time : 20 minute continue
[root@mail ~]# vi /scripts/gwping.sh
#!/bin/shipaddress=117.58.245.129/bin/ping -c 5 $ipaddress if [ $? != 0 ]; then /sbin/route del default /sbin/route add default gw 202.191.125.121 else /sbin/route del default /sbin/route add default gw 117.58.245.129 fi----------------------------------------------------------
Edit Cron Job
Execute cron job as: root
Command: /usr/bin/perl /scripts/mailqueue.pl
when mail queue reach 300 [ Q-mail ]
# vi /scripts/mailqueue.pl
# chmod 755 /scripts/mailqueue.pl
#!/usr/bin/perl#edited by Faruque Ahmed#Checks qmail queue, when mail queue reach 300 it mails to system admin.use warnings;use strict;my $queue = `qmailctl stat | sed -n "7p" | cut -d" " -f4`;if ($queue > 300){my $email = `echo "worldcm server has reached queue 300!" | mail -s "Attention needed on worldcm Mail server!" sysadmin\@worldcm.net`;}else{#system "echo 'this is test' | mail -s 'Client qmail-send process okay!' sysadmin\@worldcm.net";}---------------------------------------------
Postfix
# vi /root/Documents/mailqueue.pl
[root@mail Documents]# vi mailqueue.pl
#!/usr/bin/perl#edited by Faruque Ahmed#Checks qmail queue, when mail queue reach 300 it mails to system admin.use warnings;use strict;my $queue = `find /var/spool/postfix/deferred -type f | wc -l`;if ($queue > 100){my $email = `echo "Nassa Mail server has reached queue 100!" | mail -s "Attention needed on WorldCm Mail server-MailQ!" admin\@worldcm.net`;}else{#system "echo 'this is test' | mail -s 'Client qmail-send process okay!' sysadmin\@worldcm.net";}--------------------------
[root@mail Documents]# ./mailqueue.pl
# chmod +x mailqueue.pl
Execute cron job as: root
15,30,45,59 * * * * /root/Documents/mailqueue.pl
---
vi /root/Documents/mailqueue2.pl
chmod 755 /root/Documents/mailqueue2.pl chmod +x /root/Documents/mailqueue2.pl# crontab -e
15,30,45,59 * * * * /root/Documents/mailqueue2.pl
------BEST-------------------------------------
[root@mail Documents]# vi /root/bin/mqueue.pl
# chmod 755 /root/bin/mqueue.pl
#!/usr/bin/perluse warnings;use strict;use Net::Domain qw(hostname hostfqdn hostdomain);my $host = hostfqdn();my $queue = `/usr/sbin/postqueue -p | tail -1 | cut -d' ' -f5`;$queue =~ s/^\s+|\s+$//g;if ($queue > 150){my $todo1 = `/bin/echo "$host has currently $queue queued mail." > /root/mailqueue.txt`;my $todo2 = `/bin/echo "=======================================" >> /root/mailqueue.txt`;my $todo3 = `/usr/sbin/postqueue -p >> /root/mailqueue.txt`;my $todo4 = `/bin/mail -s "Mail Queue $queue at $host" abc\@acl.net mm\@acel.net < /root/mailqueue.txt`;}else{#system "echo 'nothing to do'";}----------------------------------- ----------------------------------------
[root@mail ~]# vi /root/Documents/mailqueue2.pl
#!/usr/bin/perluse warnings;use strict;use Net::Domain qw(hostname hostfqdn hostdomain);my $host = hostfqdn();my $queue = `/usr/sbin/postqueue -p | tail -1 | cut -d' ' -f5`;$queue =~ s/^\s+|\s+$//g;if ($queue > 150){my $todo1 = `/bin/echo "$host has currently $queue queued mail." > /root/mailqueue.txt`;my $todo2 = `/bin/echo "=======================================" >> /root/mailqueue.txt`;my $todo3 = `/usr/sbin/postqueue -p >> /root/mailqueue.txt`;my $todo4 = `/bin/mail -s "Mail Queue $queue at $host" it\@worldcm.org abc\@worldcm.org system\@worldcm.net < /root/mailqueue.txt`;}else{#system "echo 'nothing to do'";}----------BEST----------------------
[root@mail Documents]# ./mailqueue.pl
# chmod +x mailqueue.pl
--------------------------------------------------------------------
Display the list of Queued mails , deferred mails, and Pending mails
Zinbra mail log
root@mail:~# vim /usr/bin/checkmaillog.sh
---------------------------
#!/bin/bash## Checks maillog for top senders every hour#mailLog="/var/log/maillog"mailLog="/var/log/zimbra.log"
## Past hourdateTimePrefix=$( date +%b\ %_d\ %H )topTenMailersHourly=$( grep "${dateTimePrefix}" "${mailLog}" | grep "sasl_username=.*$" -o | cut -d ' ' -f 2 | sort | uniq -c | sort -n | tail -n 10 | sort -n -r )
## All daydateTimePrefix=$( date +%b\ %_d )topTenMailersDaily=$( grep "${dateTimePrefix}" "${mailLog}" | grep "sasl_username=.*$" -o | cut -d ' ' -f 2 | sort | uniq -c | sort -n | tail -n 10 | sort -n -r )
##echo -n -e "Top emailers in the past hour:\n${topTenMailersHourly}\n\nTop emailers till now today:\n${topTenMailersDaily}\n\n" | mailx -r "Monitor <monitor@worldcm.net>" -s "Email senders Report" faruq@worldcm.net
----------------------------------------
CRONTAD- root@mail
0 * * * * /usr/bin/checkmaillog.sh
# postqueue -p
Sample Output
[root@host1 ~]# postqueue -p
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
C79CEC3F6BC* 526 Wed Dec 5 15:05:18 root@host1.server.com
test.test@gmail.com
---------------------------------------------------------------------------
To check the total number of mails in the queue
[root@mail ~]# postqueue -p | grep -c "^[A-Z0-9]"
1053
# grep "This is a test" /var/log/mail.log Feb 20 11:50:16 mail postlog: This is a test
BASH: Script to alert when Exim mail queue is HIGH
-------------------------------------------
The following script sends an alert whenever the exim mail queue is greater than a certain threshold
#!/bin/bashif [ `/usr/sbin/exim -bpc` -ge 300 ]; thenmail -s "Alert: There are over 300 Emails in the queue!" email@example.com <<EOFServer Hostname :`/bin/hostname -f`Mail IP's (If Any):`cat /etc/mailips`Current Mail Queue :`/usr/sbin/exim -bpc`Server IP :`/sbin/ifconfig | /bin/awk '/inet addr/{print substr($2,6)}'`Thanks,Support Team.EOFfi
Note:
1. Set the desired threshold value for the mail queue
2. Change the alert email from email@example.com to your email ID.
-----------------------------------------------------------------
A Shell Script to Send Email Alert When Memory Gets Low
Shell Script to Check Server Memory
#!/bin/bash ####################################################################################### #Script Name :alertmemory.sh #Description :send alert mail when server memory is running low #Args : #Author :Aaron Kili Kisinga #Email :aaronkilik@gmail.com #License : GNU GPL-3 ####################################################################################### ## declare mail variables ##email subject subject="Server Memory Status Alert" ##sending mail as from="server.monitor@example.com" ## sending mail to to="admin1@example.com" ## send carbon copy to also_to="admin2@example.com" ## get total free memory size in megabytes(MB) free=$(free -mt | grep Total | awk '{print $4}') ## check if free memory is less or equals to 100MB if [[ "$free" -le 100 ]]; then ## get top processes consuming system memory and save to temporary file ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/tmp/top_proccesses_consuming_memory.txt file=/tmp/top_proccesses_consuming_memory.txt ## send email if system memory is running low echo -e "Warning, server memory is running low!\n\nFree memory: $free MB" | mailx -a "$file" -s "$subject" -r "$from" -c "$to" "$also_to" fi exit 0
After creating your script /etc/scripts/alertmemory.sh, make it executable and symlink to cron.hourly.
# chmod +x /etc/scripts/alertmemory.sh # ln -s -t /etc/cron.hourly/alertmemory.sh /etc/scripts/alertmemory.sh
This means that the above script will be run after every 1 hour as long as the server is running.
Tip: You can test if it is working as intended, set the bench mark value a little high to easily trigger an email to be sent, and specify a small interval of about 5 minutes.
Linux Mail Command Usage with Examples
$ yum install mailx -y
Mail command
The following command will allow you to send an email with the subject. The option ‘s’ allow you to an send email with subject followed by the recipient address.
$ mail -s “Enter the subject” user1@domain.com
Don’t you think something important is missing, the message! The next step before pressing the enter buttons is to type the message or if any CC (Carbon Copy) required.
After the completion of required fields press Ctrl + D, this will give an output EOT.
Send message from a file
The content of the message can be included from a file also.
$ mail -s “The Subject” user1@domain.com < /root/testemail.txt
Messages can also be including by using echo command and piping it.
$ echo “THIS IS A TEST EMAIL” | mail -s “Enter the subject” user1@domain.com
Multiple recipients
We do also have option to send the email to multiple recipients.
$ echo “THIS IS A TEST EMAIL” | mail -s “Enter the subject” user1@domain.com,user2@domain.com
CC and BCC
Blind carbon copy (BCC) and carbon copy (CC) address can also be attached within a command in Linux.
$ echo “THIS IS A TEST EMAIL” | mail -s “Enter the subject” -c user1@domain.com,user2@domain.com
Specify From name and address
$ echo “THIS IS A TEST EMAIL” | mail -s “Enter the subject” –r “Tom<tom@gmail.com>” user1@domain.com
Specify “Reply-To” address
$ echo “THIS IS A TEST EMAIL” | mail -s “Enter the subject” replyto= tom@gmail.com user1@domain.com
$ echo “THIS IS A TEST EMAIL” | mail -s “Enter the subject” replyto= “Tomtom@gmail.com” user1@domain.com
Attachments
The option –a is used to add an attachment along with the email.
$ echo “THIS IS A TEST EMAIL” | mail -s “Enter the subject” –r “Tom<tom@gmail.com>” –a /path/to/file user1@domain.com
If you need any further assistance please contact our support department.
---------