寻觅生命中的那一片浅草......

每月存档 三月, 2010

统计几个目录大小的sh

#!/bin/bash
#目录名类似于20100315,20100315long,20100316,20100316zhong,20100317等
#用法 sh count_dir_size.sh 日期最小数,日期最大数
#如果是2010-03-15到2010-03-17,则sh count_dir_size.sh 2010-03-15 2010-03-17
#Author johncan,2010-03-22
#COUNT_DATE=`date “+%Y%m%d” |cut -c1-5`
if [ -z “$1” ]
then
echo “please use $0 start_day end_day”
exit 1
fi

#将开始时间转换成时间戳
STA_TIME=`date -d “$1” +%s`
#将结束时间转换成时间戳
END_TIME=`date -d “$2” +%s`

cd /dir_which_files_are_stored

DR_TIME=${STA_TIME}
while [ “${DR_TIME}” -le “${END_TIME}” ]
do
#将时间戳转换为目录名的格式
STAND_DR_TIME=`date -d “1970-01-01 UTC ${DR_TIME} seconds” +%Y%m%d`
for DR in ${STAND_DR_TIME}*
do
#统计目录大小
DR_SIZE=`du -s ${DR} | awk ‘{print $1}’`
let “COUNT_DR_SIZE=${COUNT_DR_SIZE}+${DR_SIZE}”
done
let “DR_TIME=${DR_TIME}+86400”
done
echo ${COUNT_DR_SIZE}KB

禁止通过IP访问Jboss

cd /your_install_dir/jbossweb-tomcat50.sar/
/bin/cp server.xml server.xml.100323
vi server.xml

先修改默认的8080端口

<Service name=”jboss.web”
className=”org.jboss.web.tomcat.tc5.StandardService”>

<!– A HTTP/1.1 Connector on port 8080 –>
<!– The compression parameters are taken from the default Tomcat server.xml–>
<Connector port=”8080″ address=”${jboss.bind.address}”

将port=”8080″修改为port=”8888″

禁止通过IP访问,只允许通过域名访问

在<Host name=”localhost”
autoDeploy=”false” deployOnStartup=”false” deployXML=”false”>
前添加一个IP命名的virtual host

假如服务器IP是192.168.0.100,则添加的host如下

<Host name=”192.168.0.100″>
<Valve className=”org.apache.catalina.valves.RemoteAddrValve” deny=”0.0.0.0″/>
</Host>

现在启动Jboss,就不能通过IP访问了,只能通过域名,但任何解析到192.168.0.100的域名都可以访问
有机会再研究下只绑定一个域名的情况

参考:http://xuliangyong.javaeye.com/blog/410148

shell字符串的截取

shell字符串的截取的问题:
一、Linux shell 截取字符变量的前8位,有方法如下:
1.expr substr “$a” 1 8
2.echo $a|awk ‘{print substr(,1,8)}’
3.echo $a|cut -c1-8
4.echo $
5.expr $a : ‘\(.\\).*’
6.echo $a|dd bs=1 count=8 2>/dev/null

二、按指定的字符串截取
1、第一种方法:
${varible##*string} 从左向右截取最后一个string后的字符串
${varible#*string}从左向右截取第一个string后的字符串
${varible%%string*}从右向左截取最后一个string后的字符串
${varible%string*}从右向左截取第一个string后的字符串
“*”只是一个通配符可以不要

例子:
$ MYVAR=foodforthought.jpg
$ echo ${MYVAR##*fo}
rthought.jpg
$ echo ${MYVAR#*fo}
odforthought.jpg

2、第二种方法:${varible:n1:n2}:截取变量varible从n1到n2之间的字符串。

可以根据特定字符偏移和长度,使用另一种形式的变量扩展,来选择特定子字符串。试着在 bash 中输入以下行:
$ EXCLAIM=cowabunga
$ echo ${EXCLAIM:0:3}
cow
$ echo ${EXCLAIM:3:7}
abunga

这种形式的字符串截断非常简便,只需用冒号分开来指定起始字符和子字符串长度。

三、按照指定要求分割:
比如获取后缀名
ls -al | cut -d “.” -f2
转载自:http://tech.foolpig.com/2008/07/09/linux-shell-char/

Boot_centos5.3_into_single_user_mode_on_XenServer

Today,I clone a VM in the XenCenter.I want to log into the new VM to configure ip and so on,But I had forgotten the original VM’s root password.So I can not log into the new one.

There are at least two kinds of way to reset the root password.One way is making the new VM boot from cdrom. I think it is too complicated to do that.The other way is editing grub’s boot option and making the VM boot into single user mode.It’s easy to do that.Follow this step:choose the new VM–>Shut down the new VM–>click the right mouse button–>Properties–>Startup Options–>OS Boot Parameters–>add single after utf8,the conten after edited is graphical utf8 single.Start the new VM again,it boot into single user mode,now we can change root password.

At last,shut down the VM,change OS Boot Parameters’s content to graphical utf8,start the new VM.

Linux如何在系统启动时自动加载模块

下面是以前学习Linux时写的,后来仔细研究rc.sysinit后发现,只需要修改下列地方就可以了,不必这么麻烦的:
rc.sysinit中有这样的一段代码:
# Load other user-defined modules
for file in /etc/sysconfig/modules/*.modules ; do
[ -x $file ] && $file
done

# Load modules (for backward compatibility with VARs)
if [ -f /etc/rc.modules ]; then
/etc/rc.modules
fi

可见只需要配置两个地方的任何一个就可以了(以加载fuse内核模块为例)
(1) 在/etc/sysconfig/modules/下面创建*.modules文件,参考已经有的*.modules文件,例如我写创建文件my.modules,内容为modprobe fuse
记得最后chmod 755 my.modules
(2) 或者在/etc/rc.modules里面加上modprobe fuse,没有的话创建该文件。
然后reboot,lsmod | grep fuse验证一下就OK了。
==============

Automatically load kernel modules:

为搞清楚如何在系统启动时自动加载模块,搜索了好久,网上有很多人提出这个问题,但都没有正确的答案,无论是中文社区还是英文社区,大家的回答都没有讲到点子上,无非是围绕modprobe.conf、modprobe讲来讲去的,要不就是针对特定问题尝试不同的方法。有的还建议把modprobe modulename写入rc.local,却不曾想,rc.local的执行被放在整个启动顺序的很后面,而启动init.d下面定义的服务却在rc.local前面,那么如果某个服务要用这个模块,就不行了。

在测试LVS时,因为我的Fedora7的Kernel(2.6.21-1)缺省没有加载ip_vs模块,而内核中已经包含编译好的IPVS相关的模块了,放在:/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/下面,有:
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_dh.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_ftp.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_lblc.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_lblcr.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_lc.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_nq.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_rr.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_sed.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_sh.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_wlc.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_wrr.ko
其中ip_vs.ko是IPVS的基本模块,不加载IPVS就不能工作(运行ipvsadm会报错的),而其他的都是IPVS的调度算法或特定协议的辅助模块,需要时则须加载。

如果系统运行时手动加载则需:modprobe ip_vs 和modprobe ip_vs_sh等。

要了解如何在系统启动时自动加载模块(Automatically load kernel modules),就得先了解系统是如阿启动的,启动的过程中按什么顺序做了什么,怎么做的,这些启动操作都有那些文件和脚本控制。由于Google和Baidu出来的东西都解决不了问题,而且man modprobe和man modprobe.conf发现并不是需要修改的文件。

于是温习鸟哥的http://linux.vbird.org/“开机关机流程与Loader”:
1. 整个开机流程是
(1) 载入BIOS的硬件信息,并取得第一个开机装置的代号
(2)读取第一个开机装置的MBR的boot Loader (grub)的开机信息
(3)载入OS Kernel信息,解压Kernel,尝试驱动硬件
(4) Kernel执行init程序并获得run-lebel信息(如3或5)
(5) init执行/etc/rc.d/rc.sysinit
(6)启动内核外挂模块(/etc/modprobe.conf)
(7) init执行run-level的各种Scripts,启动服务
(8) init执行/etc/rc.d/rc.local
(9)执行/bin/login,等待用户Login
(10)Login后进入Shell

看来正确的方式是把需要加载的模块放在(5)或(6),但正如网络上很多人的尝试,修改modprobe.conf都没有成功(例如在modprobe.conf中增加install ip_vs…)。于是我修改了/etc/rc.d/rc.sysinit就成功加载了。

初步尝试在rc.sysinit最后增加 modprobe.conf ip_vs,重启后lsmod | grep ip_vs,发现成功自动加载了。

于是仿效rc.sysinit中其他模块的加载方法,扩展改脚本文件,在最后增加下来一段:
# load LVS IPVS modules
if [ -d /lib/modules/$unamer/kernel/net/ipv4/ipvs ]; then
for module in /lib/modules/$unamer/kernel/net/ipv4/ipvs/* ; do
module=${module##*/}
module=${module%.ko}
modprobe $module >/dev/null 2>&1
done
fi
就把/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/下的所有模块都自动加载了。其中:
if语句检查ipvs模块的目录是否存在
for循环遍历该目录下面的所有文件
module=${module##*/} :其中##表示从前面删除字符,*/表示删除到最后一个/,如果一个#就表示只删除到第一个/。如果变量后面接##,表示在##后面的字符串取最长的(一直到最后面),如果接#,表示取最小的一段。
module=${module%.ko}:表示从后面删除.ko。如果变量后面接%%,表示在%%后面的字符串取最长的(一直到最前面),如果接%,表示取最小的一段。
这样多module的两次修改就得到了模块名,就是文件名不带路径和.ko后缀。
modprobe $module >/dev/null 2>&1:加载模块,输出都指向空设备

这样重启后lsmod | grep ip_vs就会得到:
ip_vs_wrr 6977 0
ip_vs_wlc 6081 0
ip_vs_sh 6593 0
ip_vs_sed 6081 0
ip_vs_rr 6081 0
ip_vs_nq 5953 0
ip_vs_lc 5953 0
ip_vs_lblcr 10565 0
ip_vs_lblc 9797 0
ip_vs_ftp 10053 0
ip_vs_dh 6593 0
ip_vs 79425 22 ip_vs_wrr,ip_vs_wlc,ip_vs_sh,ip_vs_sed,ip_vs_rr,ip_vs_nq,ip_vs_lc,ip_vs_lblcr,ip_vs_lblc,ip_vs_ftp,ip_vs

转载自:http://blog.csdn.net/hansel/archive/2008/09/09/2903709.aspx

2010年三月
« 2月   4月 »
1234567
891011121314
15161718192021
22232425262728
293031