레이블이 linux인 게시물을 표시합니다. 모든 게시물 표시
레이블이 linux인 게시물을 표시합니다. 모든 게시물 표시

20140711

set linux swap

swap메모리는 주 메모리가 부족할 때 하드디스크와 같은 공간을 메모리로 사용하기 위한 가상메모리이다.(윈도우에선 가상메모리라 함)

그런데, 오늘은 주 메모리는 남아도는데 swap메모리가 부족한 어처구니없는 상황이 벌어졌으니….



머 이번 포스팅은 이런 현상의 원인이 아니라 걍 swap메모리 늘리기니깐 상황분석은 생략..(실은 잘 몰라서..-ㅅ-;;)



swap영역을 확보하기 위해서는 2가지 방법이 있다고 한다.

1. swap 파티션 구성

2. swap 파일 생성

위 2가지 중 swap 파일을 생성하는 것이 상대적으로 용이하기 때문에 swap파일을 생성, 설정, 활성화 하는 것을 이용하였다.



1. swap파일의 생성 – root(/)에 512MB의 swapfile_temp라는 것을 만든다.

# dd if=/dev/zero of=/swapfile_temp bs=1024 count=524288

              (참고 : 4G 로 생성할 경우 count=4000000)



2. swap파일 설정 - v1옵션은 new스타일, v0옵션은 old스타일이다.

# mkswap –v1 /swapfile_temp

3. swap파일 활성화

# swapon /swapfile_temp

이 후 top 또는 free명령을 통해 늘어난 swap공간을 확인할 수 있다.



* 활성화된 swap공간을 삭제하기 위해선 swapoff명령을 사용한 후 해당 파일을 삭제하면 끝~

* 리부팅시 swap 용량이 전상태로 돌아가므로 /etc/rc.d/rc.local 에 맨 마지막 명령어(swapon /swapfile_temp)를 추가해 준다.

20131020

Kill Process based on Start Time (STIME)

One of the server that I am working with has some infinitely running PHP process. Due to incorrect way of cron setup by the development team, it has caused the process hanging and not ended properly. According to them, these processes can be killed if still hang after 12 hours.
Any process which run in server will have start time (STIME). You can check this by using ps command. In this case, following result will appear:
$ ps aux | grep php
root 1399 0.0 0.0 61188 740 pts/2 S+ 10:10 0:00 grep php
user1 2697 0.0 0.0 100664 8340 ? Ss Jul04 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 5551 0.0 0.4 171052 78832 ? Ss Jun25 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 9913 0.0 0.5 174636 82392 ? Ss Jun22 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 11961 0.0 0.7 223276 131060 ? Ss May25 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 16455 0.0 0.4 171564 79420 ? Ss Jun24 0:01 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 17474 0.0 0.5 182060 90016 ? Ss Jun18 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 20094 0.0 0.6 206636 114588 ? Ss Jun03 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 22555 0.0 0.7 213548 121476 ? Ss May30 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 24670 0.0 0.7 214572 122320 ? Ss May30 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 28200 0.0 0.7 220204 127988 ? Ss May26 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 30832 0.0 0.4 170284 78168 ? Ss Jun25 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 30837 0.0 0.4 170114 88508 ? Ss 23:20 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 30848 0.0 0.4 120439 80770 ? Ss 12:20 0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
If you see the STIME value mostly has started long time ago but it is still inside the process list. To kill all the older process which more that 24 hours, I use following command:
$ kill -9 `ps aux | grep php | grep sync2server | awk '$9 !~ /[0-9]:[0-9]/' | awk '{print $2}'`
Explanation:
Using ps command, we grep anything related to “php” and “sync2server” which is the specific process that we want to kill. The 4th argument is checking whether column no 9 (STIME) column has value which NOT in “number:number” format. Process which starts for more than 24 hours, STIME value will contains word for example “Jun23″ or “2010″. The 5th argument is actually print the value of column no 2 which is the PID to be killed.
To kill process which less than 24 hours, you can use following script:
LIMIT=43200 #limit on seconds = 12 hours
PROCESS="php" #change to process u want to grep
count=`ps aux | grep $PROCESS | awk {'print $9'} | wc -l`
for ((i=1;i<=$count;i++))
do
ptime=`ps aux | grep $PROCESS | awk {'print $9'} | head -$i | tail -1`
psec=`date "+%s" -d "$ptime"`
csec=`date "+%s"`
exectime=$((csec-psec))
if [ $exectime -gt $LIMIT ]
then
pid=`ps aux | grep $PROCESS | awk {'print $2'} | head -$i | tail -1`
/bin/kill -9 $pid
fi
done

Above scripts will try to find any PHP process which executed more than 12 hours and will kill it one by one. You can get the script to run as cron twice per day so it will automate your administration job.

Lets share if you have better idea. Cheers!

Articles