当系统部署时常常需要修改目标系统的文件句柄数,如果安装的机器比较多容易忘记,这时候在应用的安装脚本中自动修改是个更好的选择,这里写了个脚本备用免得以后再去想正则表达式。在CentOS5.4下测试通过。
#cat install.sh
#/bin/bash if [ `whoami` = root ] then echo "install..." echo "set limit nofile..." LIMIT_FILE=/etc/security/limits.conf if grep "^\* *soft *nofile *[0-9]*" $LIMIT_FILE > /dev/null then sed 's/^\* *soft *nofile *[0-9]*/\* soft nofile 10240/' $LIMIT_FILE > limits.conf.temp mv limits.conf.temp $LIMIT_FILE else echo '* soft nofile 10240' >> $LIMIT_FILE fi if grep "^\* *hard *nofile *[0-9]*" $LIMIT_FILE > /dev/null then sed 's/^\* *hard *nofile *[0-9]*/\* hard nofile 32768/' $LIMIT_FILE > limits.conf.temp mv limits.conf.temp $LIMIT_FILE else echo '* hard nofile 32768' >> $LIMIT_FILE fi echo "chown run dir..." chown crust.crust /home/crust/run chmod a+x start stop showapps cp autostart.in /etc/rc.d/init.d/crust chmod a+x /etc/rc.d/init.d/crust echo "add crust autostart script" /sbin/chkconfig --add crust echo "add crust as system service" chmod u+x ./so/config_so_ld.sh ./so/config_so_ld.sh else echo "ERROR: please use root user run the script !!!!!!!" fi
脚本中首先搜索 /etc/security/limits.conf 中是否包含句柄数配置,没有的话直接在后面添加,已有配置则用sed进行替换。脚本后面还干了两件事顺带提一下:将应用配置成服务、注册动态库。
#cat autostart.in
#!/bin/bash # Init file for crust daemon # chkconfig: 35 99 01 # description: crust # processname: erl # export HOME=/home/crust/run start() { cd $HOME echo "[START] " `date` >> start.log touch /var/lock/subsys/crust >/dev/null 2>&1 su curst -c ./start >/dev/null 2>&1 } stop() { cd $HOME echo "[STOP] " `date` >> start.log rm -f /var/lock/subsys/crust >/dev/null 2>&1 su crust -c ./stop >/dev/null 2>&1 } case "$1" in start) start;; stop) stop;; esac
#cat config_so_ld.sh
#!/bin/bash if [ `whoami` = root ] then echo /home/crust/run/so > /etc/ld.so.conf.d/crust.conf echo "add config file to /etc/ld.so.conf.d" /sbin/ldconfig echo "reconfig ld" else echo "please use root user run the script" fi