How to findout which processes are swapping in linux

0

How to findout which processes are swapping in linux

The below script will give the exact swap usage result on the server.

=–=-=-===-=-=-=-
#!/bin/bash
# Get current swap usage for all running processes
# Pipe the output to “sort -nk3” to get sorted output
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d -regex “^/proc/[0-9]+”`
do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm –no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null | awk ‘{ print $2 }’`
do
let SUM=$SUM+$SWAP
done
if (( $SUM > 0 )); then
echo “PID=$PID swapped $SUM KB ($PROGNAME)”
fi
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo “Overall swap used: $OVERALL KB”
=–=-=-===-=-=-=-

The script will show which process is taking swap usage.

[~]# sh swap.sh
PID=1 swapped 80 KB (init)
PID=640 swapped 324 KB (udevd)
PID=1266 swapped 684 KB (crond)
PID=1268 swapped 492 KB (sshd)
PID=1272 swapped 16136 KB (php)
PID=2194 swapped 56 KB (iscsid)
PID=2986 swapped 44 KB (syslogd)
PID=2989 swapped 76 KB (klogd)
PID=3220 swapped 36 KB (hald-addon-stor)
PID=3268 swapped 268 KB (automount)
PID=3290 swapped 8364 KB (named)
PID=3344 swapped 152 KB (xinetd)
PID=3820 swapped 448 KB (pure-ftpd)
PID=3822 swapped 316 KB (pure-authd)
PID=3929 swapped 200 KB (crond)
PID=3942 swapped 100 KB (atd)
PID=4603 swapped 424 KB (imap-login)
Overall swap used: 194024 KB

Advertisements

Log cleaner script

0

Log cleaner script

This script will clear the apache logs, cpanel logs and the logs in /var. It will take a zipped backup before clearing logs.

#!/bin/bash

# Script to Clear Logs

echo “Clearing Logs in /var…..”

cd /var/log

for i in `ls -lSh /var/log|grep -v gz|grep “\-rw”|head -15|awk {’print $9′}`

do

cp -p “$i” “$i”.bak

echo “”>$i

if [ -e “$i”.bak.gz ]

then

mv “$i”.bak.gz “$i”.bak1.gz

fi

gzip “$i”.bak

done

echo “Clearing Cpanel logs…..”

cd /usr/local/cpanel/logs

echo “access_log error_log login_log stats_log melange.log” > /tmp/LOG

for i in `cat /tmp/LOG|cut -f1`

do

cp -p “$i” “$i”.bak

echo “”>$i

if [ -e “$i”.bak.gz ]

then

mv “$i”.bak.gz “$i”.bak1.gz

fi

gzip “$i”.bak

done

echo “Clearing Apache logs…..”

cd /usr/local/apache/logs

echo “suexec_log ssl_engine_log access_log error_log” > /tmp/LOG

for i in `cat /tmp/LOG|cut -f1`

do

cp -p “$i” “$i”.bak

echo “”>$i

if [ -e “$i”.bak.gz ]

then

mv “$i”.bak.gz “$i”.bak1.gz

fi

gzip “$i”.bak

done

echo “/var/log /usr/local/apache/logs /usr/local/cpanel/logs” > /tmp/LOG

echo “Removing Old backup logs”

for i in `cat /tmp/LOG|cut -f1`

do

cd $i

rm -f *.bak1.gz

done

rm -f /tmp/LOG

echo “Restarting Syslog…”

service syslog restart

echo “EVERY THING CLEARED ”