Friday, June 28, 2013

Linux, Who eaten up my memory?

Linux, Who eaten up my memory?

if you want to know which PID that chewing up your memory and not letting go? Here is the small bash script.

#!/bin/bash
echo
for pid in `grep -i VmSize /proc/*/status | sort -k +2n | tail -n10 | awk -F"/" '{print $3}'`
do
        echo "************************"
        echo "PID: $pid"
        echo "Allocated memory: `grep VmSize /proc/$pid/status | awk -F":" '{print $2" "$3}'`"
        echo `ps -aef | grep $pid | grep -v grep`

done
echo

Thursday, June 27, 2013

Python in Action: Search for big file in Linux

 Python in Action: Search for big file in Linux


Here is the codes. Hope that you will love it.

import os

def search():
    filesize = 0
    filename = ''
    for root, dirs, files in os.walk('/'):
        for f in files:
            f = os.path.join(root, f)
            if os.path.isfile(f):
                if os.path.getsize(f) > filesize:
                    filesize = os.path.getsize(f)
                    filename = os.path.getrealpath(f)
                    print "file: %s ==> size: %s" % (filename, filesize)
                else:
                    break
 

if __name__ == '__main__':
    search()

Friday, June 7, 2013

whoami?? whoami??

hi all,

I came across a weird case when I am working today. But, I am able to solve the problem in a minute. Just hope that I can share it out. The problem sounds as like below.

login as: hiuy
Last login: Fri Jun  7 09:01:01 2013 from 16.189.90.136
-bash: /etc/profile: Permission denied
-bash-3.2$
-bash-3.2$ id
uid=121738973 gid=6347 groups=10,6347,12132,12178
-bash-3.2$ whoami
whoami: cannot find name for user ID 121738973

[root@test ~]# ls -al /etc/passwd /etc/shadow /etc/group
-rw-r--r-- 1 root root 29575 Jun  7 08:02 /etc/passwd
-rw-r--r-- 1 root root 4368 Jun  7 08:55 /etc/group
-r-------- 1 root root 9012 Jun  7 08:02 /etc/shadow

The permission for the three important files are perfectly looks okay. So, What is the fault??

This is the hint....

-bash-3.2$ ls -al /etc/passwd
ls: /etc/passwd: Permission denied



The Answer is here...

[root@test /]# ls -al | grep etc
drwx------  89 root root 12288 Jun  7 08:55 etc


etc is having 700 only. No permission for mortal user to read and accessing /etc/passwd! So, chmod it to 755 /etc, and that's the end of the story!

This is cunning, but I like it....