Shell Script To Auto Restart HTTPD (If Goes Down)
Set con job for this script:
*/5 * * * * /path/to/script.sh >/dev/null 2>&1
===========================================================
SCRIPT:
# for RHEL / CentOS / Fedora Linux restart command
RESTART="/etc/init.d/httpd restart"
#path to pgrep command
PGREP="/usr/bin/pgrep"
# Httpd daemon name
HTTPD="httpd"
# find httpd pid
$PGREP ${HTTPD}
if [ $? -ne 0 ] # if httpd not running
then
# restart httpd
$RESTART
fi
Remote FTP backup script
This script will backup your files from a specific folder to a remote FTP location.
For each day it will create a date directory in the format of d-m-Y (ex:02-11-2008) and store your files there.
You have to create a crontab job to run at least once each day.
Type: crontab -e and paste the following line:
0 0 * * * sh /path/to/script/backup.sh | mail -s “Daily FTP Remote Backup Status” your@mail.com
Create a backup.sh file and chmod +x backup.sh to give it execute permissions.
——————————————————————————————————————-
#!/bin/bash
# FTP Settings
FTPStatus=y
HOST=’hostname’
USERNAME=’username’
PASSWORD=’password’
REMOTEDIR=`date +’%d-%m-%Y’`
HOSTNAME=’/bin/hostname’
LOCALDIR=’/path/to/backup’
### Do not edit below ###
if [ $FTPStatus = "y" ]
then
echo -e “Starting Remote FTP Backup\n”
cd $LOCALDIR
ftp -nv <
open $HOST
user $USERNAME $PASSWORD
mkdir $REMOTEDIR
cd $REMOTEDIR
prompt
binary
mput *.*
quit
EOF
echo -e “Remote FTP transfer completed\n”
fi
Turning off not required Services script
#!/bin/bash
#Dependencies services package
#Turning off un-needed services on a CentOS box
#One network NIC, no NSF, Selinux=0, no RAID, no wireless...
# VERY MINIMAL!
echo ""
echo " ********************************* "
echo " Turning un-needed things off"
echo " ********************************* "
for s in atd \
auditd \
avahi-daemon \
bluetooth \
cgconfig \
cgred \
cups \
dc_client \
dc_server \
dnsmasq \
ebtables \
firstboot \
gpsd \
haldaemon \
ip6tables \
irda \
iscsi \
iscsid \
ksm \
ksmtuned \
libvirt-guests \
libvirtd \
lvm2-monitor \
mdmonitor \
NetworkManager \
netconsole \
netfs \
nfs \
nfslock \
nmb \
ntpd \
ntpdate \
openct \
openvpn \
pcscd \
portreserve \
psacct \
rdisc \
restorecond \
rpcbind \
rpcgssd \
rpcidmapd \
rpcsvcgssd \
rsyslog \
saslauthd \
sendmail \
smb \
smolt \
snmpd \
speech-dispatcherd \
snmptrapd \
squid \
svnserve \
vboxdrv \
vboxweb-service \
wicd \
wpa_supplicant \
ypbind
do echo "chkconfig $s off";
chkconfig $s off;
done
echo ""
echo " ********************************* "
echo " Turning minimal needed things on"
echo " Httpd so webmin & mythweb work "
echo "********************************* "
for s in abrtd \
httpd \
network \
mysqld \
sshd \
udev-post \
xinetd
do echo "chkconfig $s on";
chkconfig $s on
done
echo ""
echo " ********************************* "
echo " DONE...."
echo " ********************************* "
#################################################################################
Checking Load of server in Frequent intervals
One of the major problem in linux server is rising of load to high. This can be happened due to various reason. This script will check the load of machine in frequent intervals and inform the administrator through email. This will also send the current server status.
#mkdir -p /opt/scripts
#cd /opt/scripts
#touch server_load.sh
#chmod 755 server_load.sh
Copy paste the following into the server_load.sh
#!/bin/bash
EMAIL="yourname@yourdomain.com"
SUBJECT="Alert $(hostname) load average is $L05"
TEMPFILE="/tmp/$(hostname)"
echo "Load average Crossed allowed limit." >> $TEMPFILE
echo "Hostname: $(hostname)" >> $TEMPFILE
echo "Local Date & Time : $(date)" >> $TEMPFILE
echo "| Uptime status: |" >> $TEMPFILE
echo "-----------------------------------------------------" >> $TEMPFILE
/usr/bin/uptime >> $TEMPFILE
echo "-----------------------------------------------------" >> $TEMPFILE
echo "| Top 20 CPU consuming processes: |" >> $TEMPFILE
ps aux | head -1 >> $TEMPFILE
ps aux --no-headers | sort -rn | head -20 >> $TEMPFILE
echo "| Top 10 memory-consuming processes: |" >> $TEMPFILE
ps aux --no-headers| sort -rn | head >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
echo "| Memory and Swap status: |" >> $TEMPFILE
/usr/bin/free -m >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
echo "| Active network connection: |" >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
/bin/netstat -tnup | grep ESTA >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
echo "| Disk Space information: |" >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
/bin/df -h >> $TEMPFILE
echo "-----------------THE END----------------------------" >> $TEMPFILE
#Store the Current Load into a variable
L05="$(uptime|awk '{print $(NF-2)}'|cut -d. -f1)"
#Checking whether it goes beyond the limit
if test $L05 -gt 0
then
mail -s "$SUBJECT $L05" "$EMAIL" < $TEMPFILE fi #Remove the Temporary file.
rm -f $TEMPFILE
Create CronJob for this
#vi /etc/crontab
#The following script will run in every minute.
*/1 * * * * root /opt/scripts/server_load.sh
#################################################################################
Monitoring Disk Usage In Linux
One of the routine job of a Linux administrator to monitor the Disk space continuously. Normal people will write a simple script to check the disk space and report to system administrator through email. Good Idea,but here i am telling a new one which will continuously monitor your Disk and report if it cross a preset value. This will run as a cronjob.#!/bin/bash
#Script for monitoring Disk Usage
#Author BipinDas,Arab Open University.
ADMIN="yourname@yourdomain.com"
# set alert level 80% is default
ALERT=80
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
#echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) $(hostname -i) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep " $ADMIN
fi
done
Save it as disk_monitor.sh in your scripts folder.
Open Crontab Editor
vi /etc/crontab
*/5 * * * * root /path/to/script/disk_monitor.sh
Perfect,This will frequently check your Disk and inform once it cross the limit.
No comments:
Post a Comment