Subscribed unsubscribe Subscribe Subscribe

How to increase ulimit on #Gentoo ?

追記

Gentoo忘年会ー勉強会 2014-12-20 - connpassGentoo Advent Calendar 2014 - Qiitaの存在を知りましたのでお酒を呑みながら慎重に検討した結果この記事は21日目に再配置されました。 ときにはそういうことも必要だと思います!

20日目は@aliceinwireさんでした。 22日目は@kizononnさんです。

追記ここまで


Linuxサーバーを運用していると"Too many open files"に出くわし"ulimit 反映されない"でググって Linux サーバでの「Too many open files」対策について - akishin999の日記ulimitが効かない不安を無くす設定 | 外道父の匠 のお世話になるのはままある話。

Gentooの場合、起動スクリプトで起動するプロセス(いわゆるdaemon)であれば簡単にulimitを設定できる。


たとえばRedmineをdaemon化するこんな起動スクリプトを書いたとして、

[root@redmine-host] # cat /etc/init.d/redmine
#!/sbin/runscript
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

description="Redmine daemon"

REDMINE_ENVIRONMENT="${REDMINE_ENVIRONMENT:-production}"
REDMINE_USER="${REDMINE_USER:-redmine}"
REDMINE_GROUP="${REDMINE_GROUP:-redmine}"
REDMINE_NAME="${SVCNAME##*.}"
if [ -n "${REDMINE_NAME}" -a "${SVCNAME}" != "redmine" ]; then
  description="${description} for ${REDMINE_NAME}"
  REDMINE_PID_FILE="/var/run/redmine.${REDMINE_NAME}.pid"
  REDMINE_RB_DEFAULT="/etc/redmine/${REDMINE_NAME}.rb"
else
  REDMINE_PID_FILE="/var/run/redmine.pid"
  REDMINE_RB_DEFAULT="/etc/redmine/unicorn.rb"
fi
REDMINE_DIR="${REDMINE_DIR:-/var/lib/redmine}"
REDMINE_RB="${REDMINE_RB:-${REDMINE_RB_DEFAULT}}"
REDMINE_WORKER_PROCESSES="${REDMINE_WORKER_PROCESSES:-4}"
REDMINE_BUNDLE_EXEC="${REDMINE_BUNDLE_EXEC:-/usr/local/bin/bundle}"
LD_PRELOAD_OPTS=""
if [ -f "/usr/lib64/libjemalloc.so" ]; then
  LD_PRELOAD_OPTS="/usr/lib64/libjemalloc.so"
fi

depend() {
  need net
}

start() {
  ebegin "Starting ${SVCNAME}"
  start-stop-daemon \
    --start \
    --chdir "${REDMINE_DIR}" \
    --user "${REDMINE_USER}" \
    --background \
    --make-pidfile --pidfile "${REDMINE_PID_FILE}" \
    --env PATH="${PATH}:$(dirname ${REDMINE_BUNDLE_EXEC})" \
    --env LD_PRELOAD="${LD_PRELOAD_OPTS}" \
    --env REDMINE_PID_FILE="${REDMINE_PID_FILE}" \
    --exec "${REDMINE_BUNDLE_EXEC}" \
    -- exec unicorn_rails -c "${REDMINE_RB}" -E "${REDMINE_ENVIRONMENT}"
  eend $?
}

stop() {
  ebegin "Stopping ${SVCNAME}"
  start-stop-daemon --stop --pidfile "${REDMINE_PID_FILE}"
  eend $?
}

/etc/conf.d/SVCNAMErc_ulimitを書いておくと反映してくれる。

[root@redmine-host] # cat /etc/conf.d/redmine
rc_ulimit="-n 65535"
rc_need="nginx"

REDMINE_DIR="/var/lib/redmine/versions/current"
REDMINE_BUNDLE_EXEC="/usr/local/bin/bundle"
REDMINE_RB="/var/lib/redmine/versions/current/config/unicorn.rb"

便利!

[root@redmine-host] # cat /proc/$(cat /var/run/redmine.pid)/limits
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             3981                 3981                 processes 
Max open files            65535                65535                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       3981                 3981                 signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        

ちなみにcomment outすると

[root@redmine-host] # grep rc_ulimit /etc/conf.d/redmine
#rc_ulimit="-n 65535"

こんな感じ。

[root@redmine-host] # cat /proc/$(cat /var/run/redmine.pid)/limits
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             3981                 3981                 processes 
Max open files            1024                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       3981                 3981                 signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us