<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>朱文昊 Albert Zhu &#187; 自由软件社区</title>
	<atom:link href="http://zhuwenhao.com/category/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/feed/" rel="self" type="application/rss+xml" />
	<link>http://zhuwenhao.com</link>
	<description>朱文昊的中文博客－－专注技术，向往自由</description>
	<lastBuildDate>Wed, 25 Aug 2010 00:53:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>自己动手制作U3P安装软件</title>
		<link>http://zhuwenhao.com/545/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/%e8%87%aa%e5%b7%b1%e5%8a%a8%e6%89%8b%e5%88%b6%e4%bd%9cu3p%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/</link>
		<comments>http://zhuwenhao.com/545/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/%e8%87%aa%e5%b7%b1%e5%8a%a8%e6%89%8b%e5%88%b6%e4%bd%9cu3p%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 08:37:57 +0000</pubDate>
		<dc:creator>朱文昊 Albert Zhu</dc:creator>
				<category><![CDATA[自由软件社区]]></category>

		<guid isPermaLink="false">http://zhuwenhao.com/?p=545</guid>
		<description><![CDATA[使用PackageFactory for U3可以自己制作U3软件安装到自己的U3优盘上.
软件下载地址: http://www.eure.ca/
因为很多软件在设计的时候都会保存信息到Documents目录或者使用注册表项,所以从零开始制作优盘移动软件很难,幸好有Portable  Apps项目,该项目已经把很多热门软件移动化,我利用这些程序制作自己的U3软件,就非常简单了.  首先下载需要的软件,安装,(路径可以选择安装在硬盘上,安装速度快)
本文末尾提供了我制作的二十几个u3p文件,您可以直接下载.
使用方法:
1,启动界面

2,点击右下角Advanced Mode (Wizard Mode我不会用,会出错)进入下图界面


3,点击Host Folder,然后Add File: 找到Portable Apps安装目录下面的主exe文件,例如Songbird  Portable.exe

4,程序自己解析该exe,提取名称,图标,版本信息等

5,再返回Host Folder

6,增加Portable Apps 目录下所有相关的子目录

7,回到Summary界面,点击Create a U3P file, 界面如下,过一会儿一个你自己制作的U3安装软件.u3p就出来了.

这个.u3p就可以安装在U3launchpad里面了.
下面提供我制作的文件,省得大家重复劳动.

]]></description>
			<content:encoded><![CDATA[<p>使用PackageFactory for U3可以自己制作U3软件安装到自己的U3优盘上.</p>
<p>软件下载地址: <a href="http://www.eure.ca/">http://www.eure.ca/</a></p>
<p>因为很多软件在设计的时候都会保存信息到Documents目录或者使用注册表项,所以从零开始制作优盘移动软件很难,幸好有<a href="http://portableapps.com/apps" target="_blank">Portable  Apps</a>项目,该项目已经把很多热门软件移动化,我利用这些程序制作自己的U3软件,就非常简单了.  首先下载需要的软件,安装,(路径可以选择安装在硬盘上,安装速度快)</p>
<p>本文末尾提供了我制作的二十几个u3p文件,您可以直接下载.</p>
<p>使用方法:</p>
<p>1,启动界面</p>
<p><a href="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_4.png"><img class="alignnone size-medium wp-image-527" title="WindowsLiveWriterU3APP_AE5Bimage_4.png" src="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_4-300x222.png" alt="" width="300" height="222" /></a></p>
<p>2,点击右下角Advanced Mode (Wizard Mode我不会用,会出错)进入下图界面</p>
<p><span id="more-545"></span></p>
<p><a href="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_8.png"><img class="alignnone size-medium wp-image-529" title="WindowsLiveWriterU3APP_AE5Bimage_8.png" src="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_8-300x222.png" alt="" width="300" height="222" /></a></p>
<p>3,点击Host Folder,然后Add File: 找到Portable Apps安装目录下面的主exe文件,例如Songbird  Portable.exe</p>
<p><a href="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_10.png"><img class="alignnone size-medium wp-image-531" title="WindowsLiveWriterU3APP_AE5Bimage_10.png" src="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_10-300x222.png" alt="" width="300" height="222" /></a></p>
<p>4,程序自己解析该exe,提取名称,图标,版本信息等</p>
<p><a href="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_12.png"><img class="alignnone size-medium wp-image-533" title="WindowsLiveWriterU3APP_AE5Bimage_12.png" src="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_12-300x222.png" alt="" width="300" height="222" /></a></p>
<p>5,再返回Host Folder</p>
<p><a href="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_14.png"><img class="alignnone size-medium wp-image-535" title="WindowsLiveWriterU3APP_AE5Bimage_14.png" src="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_14-300x222.png" alt="" width="300" height="222" /></a></p>
<p>6,增加Portable Apps 目录下所有相关的子目录</p>
<p><a href="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_16.png"><img class="alignnone size-medium wp-image-537" title="WindowsLiveWriterU3APP_AE5Bimage_16.png" src="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_16-300x222.png" alt="" width="300" height="222" /></a></p>
<p>7,回到Summary界面,点击Create a U3P file, 界面如下,过一会儿一个你自己制作的U3安装软件.u3p就出来了.</p>
<p><a href="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_18.png"><img class="alignnone size-medium wp-image-539" title="WindowsLiveWriterU3APP_AE5Bimage_18.png" src="http://zhuwenhao.com/wp-content/uploads/2010/06/WindowsLiveWriterU3APP_AE5Bimage_18-300x222.png" alt="" width="300" height="222" /></a></p>
<p>这个.u3p就可以安装在U3launchpad里面了.</p>
<p>下面提供我制作的文件,省得大家重复劳动.<br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=7-ZipPortable" title=" downloaded 20 times" >7-ZipPortable (20)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=AudacityPortable" title=" downloaded 14 times" >AudacityPortable (14)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=BonkEncPortable" title=" downloaded 14 times" >BonkEncPortable (14)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=ClamWinPortable" title=" downloaded 14 times" >ClamWinPortable (14)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=ConvertAllPortable" title=" downloaded 13 times" >ConvertAllPortable (13)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=EraserDropPortable" title=" downloaded 16 times" >EraserDropPortable (16)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=EraserPortable" title=" downloaded 12 times" >EraserPortable (12)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=FoxitReaderPortable" title=" downloaded 18 times" >FoxitReaderPortable (18)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=GIMPPortable" title=" downloaded 11 times" >GIMPPortable (11)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=GoogleChromePortable" title=" downloaded 16 times" >GoogleChromePortable (16)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=GVimPortable" title=" downloaded 12 times" >GVimPortable (12)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=InfraRecorder+Portable" title=" downloaded 16 times" >InfraRecorder Portable (16)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=MirandaPortable" title=" downloaded 11 times" >MirandaPortable (11)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=MozillaFirefoxPortableEdition" title=" downloaded 14 times" >MozillaFirefoxPortableEdition (14)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=MozillaThunderbirdPortableEdition" title=" downloaded 15 times" >MozillaThunderbirdPortableEdition (15)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=NotepadPortable" title=" downloaded 13 times" >NotepadPortable (13)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=PidginPortable" title=" downloaded 10 times" >PidginPortable (10)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=PuTTYPortable" title=" downloaded 13 times" >PuTTYPortable (13)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=SongbirdPortable" title=" downloaded 12 times" >SongbirdPortable (12)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=SpyDLLRemoverPortable" title=" downloaded 13 times" >SpyDLLRemoverPortable (13)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=SumatraPDFPortable" title=" downloaded 12 times" >SumatraPDFPortable (12)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=TeamViewerPortable" title=" downloaded 13 times" >TeamViewerPortable (13)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=WinDirStatPortable" title=" downloaded 15 times" >WinDirStatPortable (15)</a><br />
<br />
<a class="downloadlink" href="http://zhuwenhao.com/wp-content/plugins/download-monitor/download.php?id=WinMergePortable" title=" downloaded 13 times" >WinMergePortable (13)</a><br /></p>
]]></content:encoded>
			<wfw:commentRss>http://zhuwenhao.com/545/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/%e8%87%aa%e5%b7%b1%e5%8a%a8%e6%89%8b%e5%88%b6%e4%bd%9cu3p%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>监控剩余内存,自动重启Apache和Mysql,使用Crontab和Daemon方法.</title>
		<link>http://zhuwenhao.com/257/%e6%8a%80%e6%9c%af/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%92%8c%e7%bd%91%e7%ab%99%e7%b1%bb/%e7%9b%91%e6%8e%a7%e5%89%a9%e4%bd%99%e5%86%85%e5%ad%98%e8%87%aa%e5%8a%a8%e9%87%8d%e5%90%afapache%e5%92%8cmysql%e4%bd%bf%e7%94%a8crontab%e5%92%8cdaemon%e6%96%b9%e6%b3%95/</link>
		<comments>http://zhuwenhao.com/257/%e6%8a%80%e6%9c%af/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%92%8c%e7%bd%91%e7%ab%99%e7%b1%bb/%e7%9b%91%e6%8e%a7%e5%89%a9%e4%bd%99%e5%86%85%e5%ad%98%e8%87%aa%e5%8a%a8%e9%87%8d%e5%90%afapache%e5%92%8cmysql%e4%bd%bf%e7%94%a8crontab%e5%92%8cdaemon%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 07:27:02 +0000</pubDate>
		<dc:creator>朱文昊 Albert Zhu</dc:creator>
				<category><![CDATA[服务器和网站类]]></category>
		<category><![CDATA[自由软件社区]]></category>

		<guid isPermaLink="false">http://zhuwenhao.com/257/%e6%8a%80%e6%9c%af/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%92%8c%e7%bd%91%e7%ab%99%e7%b1%bb/%e7%9b%91%e6%8e%a7%e5%89%a9%e4%bd%99%e5%86%85%e5%ad%98%e8%87%aa%e5%8a%a8%e9%87%8d%e5%90%afapache%e5%92%8cmysql%e4%bd%bf%e7%94%a8crontab%e5%92%8cdae</guid>
		<description><![CDATA[写下文本，源于我最近一次失败的尝试。我的VPS是限定最高内存用量的，上限为2G，如果内存用完了，就会出现Cannot allocate memory无法获取可用内存的故障，导致SSH无法远程登录，也网站性能也急剧下降。这个问题可以通过限制Apache和Mysql的配置来预防。可以做保守配置来杜绝这样的问题。但是由于我的配置比较乐观，可能在某些木马攻击或者劣质网站爬虫的骚扰下，前几天出现过一次内存不足问题，导致不得不请求服务商帮我重启了一下运行了60多天的服务器，让我非常伤心。所以我尝试了一下，可否通过建立脚本，监控运行状况来自动重启Apache和Mysql来释放内存，避免重启服务器，好让我的uptime千秋万代地积累下去?
最容易想到的就是采用Crontab来做了，下面是我第一次的尝试。脚本如下

View Code BASH1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#check free memory and decide how to maintain system.
#Albert Zhu 朱文昊 V0.1 http://zhuwenhao.com
MinFreeMem='100000' #only restart apache below this number
BadFreeMem='10000'  #retart apache and mysql below this number
CurrentMem=$&#40;cat /proc/meminfo &#124; grep -i MemFree &#124; cut -d ':' -f2 &#124; cut -d 'k' -f1&#41;
CurrentMem=$&#40;&#40;$CurrentMem+0&#41;&#41;
#echo &#34;CurrentMem=&#34;$CurrentMem
#echo &#34;MinFreeMem=&#34;$MinFreeMem
#echo &#34;BadFreeMem=&#34;$BadFreeMem
if &#91; $BadFreeMem -gt $CurrentMem &#93;;
then
  ]]></description>
			<content:encoded><![CDATA[<p>写下文本，源于我最近一次失败的尝试。我的VPS是限定最高内存用量的，上限为2G，如果内存用完了，就会出现Cannot allocate memory无法获取可用内存的故障，导致SSH无法远程登录，也网站性能也急剧下降。这个问题可以通过限制Apache和Mysql的配置来预防。可以做保守配置来杜绝这样的问题。但是由于我的配置比较乐观，可能在某些木马攻击或者劣质网站爬虫的骚扰下，前几天出现过一次内存不足问题，导致不得不请求服务商帮我重启了一下运行了60多天的服务器，让我非常伤心。所以我尝试了一下，可否通过建立脚本，监控运行状况来自动重启Apache和Mysql来释放内存，避免重启服务器，好让我的uptime千秋万代地积累下去?</p>
<p>最容易想到的就是采用Crontab来做了，下面是我第一次的尝试。脚本如下</p>
<p>
<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p257code4'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2574"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p257code4"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#check free memory and decide how to maintain system.</span>
<span style="color: #666666; font-style: italic;">#Albert Zhu 朱文昊 V0.1 http://zhuwenhao.com</span>
<span style="color: #007800;">MinFreeMem</span>=<span style="color: #ff0000;">'100000'</span> <span style="color: #666666; font-style: italic;">#only restart apache below this number</span>
<span style="color: #007800;">BadFreeMem</span>=<span style="color: #ff0000;">'10000'</span>  <span style="color: #666666; font-style: italic;">#retart apache and mysql below this number</span>
<span style="color: #007800;">CurrentMem</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>meminfo <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-i</span> MemFree <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">':'</span> <span style="color: #660033;">-f2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">'k'</span> -f1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">CurrentMem</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$CurrentMem</span>+<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #666666; font-style: italic;">#echo &quot;CurrentMem=&quot;$CurrentMem</span>
<span style="color: #666666; font-style: italic;">#echo &quot;MinFreeMem=&quot;$MinFreeMem</span>
<span style="color: #666666; font-style: italic;">#echo &quot;BadFreeMem=&quot;$BadFreeMem</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$BadFreeMem</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$CurrentMem</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d-%H-%M-%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;CurrentMem=&quot;</span><span style="color: #007800;">$CurrentMem</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;restarted two service&quot;</span>
        service apache2 restart;
        service mysql   restart;
<span style="color: #000000; font-weight: bold;">else</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$MinFreeMem</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$CurrentMem</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
        <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d-%H-%M-%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;CurrentMem=&quot;</span><span style="color: #007800;">$CurrentMem</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;restarted apache&quot;</span>
                service apache2 restart;
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d-%H-%M-%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Free Memory is Good!&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

</p>
<p>然后使用cron job来自动运行 crontab –e 后加入</p>
<p>
<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p257code5'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2575"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p257code5"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> .<span style="color: #000000; font-weight: bold;">/</span>memmonitor.sh <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> .<span style="color: #000000; font-weight: bold;">/</span>memmonitor.log</pre></td></tr></table></div>

</p>
<p>这样我的脚本会每分钟运行一下，怎么样？感觉很爽吧！于是压力测试，Apache在强大的并发连接下，很快内存飙升，2G内存立马没了。结果，当Cron job醒来后，内存不足，自己都运行不起来，崩溃~~~</p>
<p>分析一下，感觉是cron job每分钟运行一次，太慢！造成对内存感觉不灵敏，等到发现，已经晚了。cron设计最快也就是每分钟运行。于是改用死循环来做，也就是做成守护进程Daemon，对内存每秒钟检查一次，够好了吧？</p>
<p>继续压力测试，依然崩溃~~~~~</p>
<p>于是review脚本，发现 service apache2 restart这样的语句，本身需要较大的内存，而且貌似要等到apache的子进程把当前服务提供完后，才终止（这点我不是很确定，不要太相信）。如果在DDOS下，哪里有空等服务结束？应当直接kill掉。修改脚本为如下</p>
<p>
<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p257code6'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2576"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code" id="p257code6"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#check free memory and decide how to maintain system.</span>
<span style="color: #666666; font-style: italic;">#Albert Zhu 朱文昊 V0.2 http://zhuwenhao.com</span>
KillAllApacheSon <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #007800;">ListPIDtoKill</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ps</span>  <span style="color: #660033;">-u</span> wwwrun <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> httpd <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">' '</span> -f1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;List PID to Kill &quot;</span><span style="color: #007800;">$ListPIDtoKill</span>
<span style="color: #007800;">i</span>=<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #007800;">GetPIDtoKill</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #800000;">${ListPIDtoKill}</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">' '</span> <span style="color: #660033;">-f</span><span style="color: #800000;">${i}</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">GetPIDtoKill</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$GetPIDtoKill</span>+<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$GetPIDtoKill</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
        <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #007800;">$GetPIDtoKill</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;kill &quot;</span><span style="color: #007800;">$GetPIDtoKill</span>
                <span style="color: #007800;">i</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$i</span>+<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #7a0874; font-weight: bold;">break</span>;
        <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Killed Apache Son Num: &quot;</span><span style="color: #007800;">$i</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
KillMysql <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #007800;">GetPIDtoKill</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-u</span> mysql <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">' '</span> -f1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">GetPIDtoKill</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$GetPIDtoKill</span>+<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$GetPIDtoKill</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #007800;">$GetPIDtoKill</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;kill mysql and PID is &quot;</span><span style="color: #007800;">$GetPIDtoKill</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
CheckMem <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #007800;">MinFreeMem</span>=<span style="color: #ff0000;">'100000'</span> <span style="color: #666666; font-style: italic;">#only restart apache below this number</span>
<span style="color: #007800;">BadFreeMem</span>=<span style="color: #ff0000;">'10000'</span>  <span style="color: #666666; font-style: italic;">#retart apache and mysql below this number</span>
<span style="color: #007800;">CurrentMem</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>meminfo <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-i</span> MemFree <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">':'</span> <span style="color: #660033;">-f2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">'k'</span> -f1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">CurrentMem</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$CurrentMem</span>+<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #666666; font-style: italic;">#echo &quot;CurrentMem=&quot;$CurrentMem</span>
<span style="color: #666666; font-style: italic;">#echo &quot;MinFreeMem=&quot;$MinFreeMem</span>
<span style="color: #666666; font-style: italic;">#echo &quot;BadFreeMem=&quot;$BadFreeMem</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$BadFreeMem</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$CurrentMem</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d-%H-%M-%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;CurrentMem=&quot;</span><span style="color: #007800;">$CurrentMem</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;restarted two service&quot;</span>
        KillAllApacheSon
        KillMysql
        service mysql   restart
<span style="color: #000000; font-weight: bold;">else</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$MinFreeMem</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$CurrentMem</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
        <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d-%H-%M-%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;CurrentMem=&quot;</span><span style="color: #007800;">$CurrentMem</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;restarted apache&quot;</span>
                KillAllApacheSon
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d-%H-%M-%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Free Memory is Good!&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
renice <span style="color: #660033;">-15</span> <span style="color: #007800;">$$</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> .<span style="color: #000000; font-weight: bold;">/</span>second.log
<span style="color: #666666; font-style: italic;">#echo $(nice)</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #666666; font-style: italic;">#       sleep 1</span>
        usleep <span style="color: #000000;">100</span>
        CheckMem<span style="color: #000000; font-weight: bold;">&gt;&gt;</span>.<span style="color: #000000; font-weight: bold;">/</span>second.log
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

</p>
<p>这次修改比较幸苦，感觉一个小脚本写到这份上，对我来说也算得上是高级bash编程了吧？呵呵。拉出来溜溜？惨不忍睹。依然无法实现预期功能。为什么？难道1秒钟也太漫长？</p>
<p>于是请出usleep，每隔100毫秒就检查内存一次。我的cpu 10%都用在这个脚本上了，结果怎么样？没用！ 写到这里，我心灰意冷了，彻底放弃。亲爱的朋友，有谁能帮帮我呢？</p>
<p>我写出上面的文字来，除了期待高手来解救我之外，还有一个原因。仔细想想看，我的这些脚本不能在VPS上实现功能，但应该可以在独立服务器或者PC上工作，为什么呢？因为VPS一旦接近耗尽内存，那真的是一点memory都allocate不处理,所以脚本本身就无法继续执行.但是对于独立服务器或者PC, 都有swap机制. 在物理内存耗尽的情况下, 会拿硬盘来充数,虽然速度慢很多,但以上的两个版本的脚本,都有可能正常工作.所以如果你测试以上脚本可用,请告诉我,抚慰一下我受伤的心吧 <img src='http://zhuwenhao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://zhuwenhao.com/257/%e6%8a%80%e6%9c%af/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%92%8c%e7%bd%91%e7%ab%99%e7%b1%bb/%e7%9b%91%e6%8e%a7%e5%89%a9%e4%bd%99%e5%86%85%e5%ad%98%e8%87%aa%e5%8a%a8%e9%87%8d%e5%90%afapache%e5%92%8cmysql%e4%bd%bf%e7%94%a8crontab%e5%92%8cdaemon%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[建站日记]]></series:name>
	</item>
		<item>
		<title>发布我的wordpress主题arclite的中文翻译</title>
		<link>http://zhuwenhao.com/139/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/%e5%8f%91%e5%b8%83%e6%88%91%e7%9a%84wordpress%e4%b8%bb%e9%a2%98arclite%e7%9a%84%e4%b8%ad%e6%96%87%e7%bf%bb%e8%af%91/</link>
		<comments>http://zhuwenhao.com/139/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/%e5%8f%91%e5%b8%83%e6%88%91%e7%9a%84wordpress%e4%b8%bb%e9%a2%98arclite%e7%9a%84%e4%b8%ad%e6%96%87%e7%bf%bb%e8%af%91/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 03:19:00 +0000</pubDate>
		<dc:creator>朱文昊 Albert Zhu</dc:creator>
				<category><![CDATA[自由软件社区]]></category>

		<guid isPermaLink="false">http://zhuwenhao.com/139/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/%e5%8f%91%e5%b8%83%e6%88%91%e7%9a%84wordpress%e4%b8%bb%e9%a2%98arclite%e7%9a%84%e4%b8%ad%e6%96%87%e7%bf%bb%e8%af%91/</guid>
		<description><![CDATA[Arclite 2.02 作者为 digitalnature, 是一款有非常多的定制选项的wordpress主题, 我非常喜欢它的自适应屏幕的设计. 这几天我把它翻译了一下, 本来自己用. 不过既然是GPL许可证的主题, 我也就不私藏了, 把拙作贡献出来, 希望有用到的朋友多提意见.
这是第一次发布,版本号就叫 Ver0.1吧, 前台的单词都翻译完了, 后台配置界面很多没有翻译,如果有需要我会继续翻译的.
请在http://svn.zhuwenhao.com/arclite/ 下载po和mo文件,放到自己的themes/arclite/lang目录下就一切OK了.
]]></description>
			<content:encoded><![CDATA[<p>Arclite 2.02 作者为 digitalnature, 是一款有非常多的定制选项的wordpress主题, 我非常喜欢它的自适应屏幕的设计. 这几天我把它翻译了一下, 本来自己用. 不过既然是GPL许可证的主题, 我也就不私藏了, 把拙作贡献出来, 希望有用到的朋友多提意见.</p>
<p>这是第一次发布,版本号就叫 Ver0.1吧, 前台的单词都翻译完了, 后台配置界面很多没有翻译,如果有需要我会继续翻译的.</p>
<p>请在<a href="http://svn.zhuwenhao.com/arclite/">http://svn.zhuwenhao.com/arclite/</a> 下载po和mo文件,放到自己的themes/arclite/lang目录下就一切OK了.</p>
]]></content:encoded>
			<wfw:commentRss>http://zhuwenhao.com/139/%e6%8a%80%e6%9c%af/%e8%87%aa%e7%94%b1%e8%bd%af%e4%bb%b6%e7%a4%be%e5%8c%ba/%e5%8f%91%e5%b8%83%e6%88%91%e7%9a%84wordpress%e4%b8%bb%e9%a2%98arclite%e7%9a%84%e4%b8%ad%e6%96%87%e7%bf%bb%e8%af%91/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
