Permalink
Browse files

Site updated at 2012-06-18 16:23:32 UTC

  • Loading branch information...
1 parent d6828eb commit 2bec6ba8f17d0e67c98a44cd1edfec7114d6ff8d @xbot committed Jun 18, 2012
View
@@ -4,7 +4,7 @@
<title><![CDATA[牧碼志]]></title>
<link href="http://0x3f.org/atom.xml" rel="self"/>
<link href="http://0x3f.org/"/>
- <updated>2012-06-14T23:03:23+08:00</updated>
+ <updated>2012-06-19T00:22:27+08:00</updated>
<id>http://0x3f.org/</id>
<author>
<name><![CDATA[晴耕雨讀]]></name>
@@ -14,6 +14,177 @@
<entry>
+ <title type="html"><![CDATA[怎樣為紅帽系Linux發行版寫系統服務腳本]]></title>
+ <link href="http://0x3f.org/blog/how-to-write-service-scripts-for-redhat-distros/"/>
+ <updated>2012-06-19T00:14:00+08:00</updated>
+ <id>http://0x3f.org/blog/how-to-write-service-scripts-for-redhat-distros</id>
+ <content type="html"><![CDATA[<h2>閱讀說明</h2>
+
+<p>閱讀本文要求有基本的Linux系統使用經驗和Bash腳本編程能力。</p>
+
+<p>本文所述的內容適用於RHEL、CentOS等紅帽系Linux發行版。</p>
+
+<h2>系統服務簡介</h2>
+
+<h3>常用命令</h3>
+
+<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+<span class='line-number'>7</span>
+<span class='line-number'>8</span>
+<span class='line-number'>9</span>
+<span class='line-number'>10</span>
+<span class='line-number'>11</span>
+<span class='line-number'>12</span>
+<span class='line-number'>13</span>
+<span class='line-number'>14</span>
+<span class='line-number'>15</span>
+<span class='line-number'>16</span>
+<span class='line-number'>17</span>
+<span class='line-number'>18</span>
+<span class='line-number'>19</span>
+<span class='line-number'>20</span>
+</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># 啟動MySQL</span>
+</span><span class='line'>service mysqld start
+</span><span class='line'>
+</span><span class='line'><span class="c"># 關閉MySQL</span>
+</span><span class='line'>service mysqld stop
+</span><span class='line'>
+</span><span class='line'><span class="c"># 重啟MySQL</span>
+</span><span class='line'>service mysqld restart
+</span><span class='line'>
+</span><span class='line'><span class="c"># 查看MySQL服務運行狀態</span>
+</span><span class='line'>service mysqld status
+</span><span class='line'>
+</span><span class='line'><span class="c"># 查看MySQL服務是否開機自動運行</span>
+</span><span class='line'>chkconfig --list mysqld
+</span><span class='line'>
+</span><span class='line'><span class="c"># 設置MySQL服務開機自動運行</span>
+</span><span class='line'>chkconfig --level 345 mysqld on
+</span><span class='line'>
+</span><span class='line'><span class="c"># 取消MySQL服務開機自動運行</span>
+</span><span class='line'>chkconfig --level 345 mysqld off
+</span></code></pre></td></tr></table></div></figure>
+
+
+<h3>運行級別</h3>
+
+<p>泛UNIX派系操作系統使用運行級別標識使用何種模式初始化。理論上,不同的發行版各級別代表的意義不同。紅帽系發行版使用以下七個運行級別:</p>
+
+<ul>
+<li>0:關機</li>
+<li>1:單用戶模式</li>
+<li>2:多用戶模式,無網絡服務</li>
+<li>3:多用戶模式,有網絡服務</li>
+<li>4:未使用/用戶自定義</li>
+<li>5:帶圖形界面的多用戶模式</li>
+<li>6:重啟</li>
+</ul>
+
+
+<p>最常用的是0、3、5、6四種模式。其中,0和6表示關閉和重啟操作系統,所以,執行命令<strong>init 0</strong>和<strong>shutdown</strong>是一個效果,同樣,執行命令<strong>init 6</strong>和<strong>reboot</strong>是一個效果。模式3即普通的命令行用戶界面,模式5即普通的圖形界面用戶界面。</p>
+
+<p>模式1在個別情景下會用到,例如忘記root用戶的密碼時,需要進入模式1修改密碼。</p>
+
+<h2>系統服務腳本格式</h2>
+
+<h3>示例</h3>
+
+<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+<span class='line-number'>7</span>
+<span class='line-number'>8</span>
+<span class='line-number'>9</span>
+<span class='line-number'>10</span>
+<span class='line-number'>11</span>
+<span class='line-number'>12</span>
+<span class='line-number'>13</span>
+<span class='line-number'>14</span>
+<span class='line-number'>15</span>
+<span class='line-number'>16</span>
+<span class='line-number'>17</span>
+<span class='line-number'>18</span>
+<span class='line-number'>19</span>
+<span class='line-number'>20</span>
+<span class='line-number'>21</span>
+<span class='line-number'>22</span>
+<span class='line-number'>23</span>
+<span class='line-number'>24</span>
+<span class='line-number'>25</span>
+<span class='line-number'>26</span>
+</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/bash</span>
+</span><span class='line'>
+</span><span class='line'><span class="c"># chkconfig: 345 99 12</span>
+</span><span class='line'><span class="c"># description: This is a sample service script</span>
+</span><span class='line'>
+</span><span class='line'><span class="k">case</span> <span class="s2">&quot;$1&quot;</span> in
+</span><span class='line'> start<span class="o">)</span>
+</span><span class='line'> <span class="c"># Start something.</span>
+</span><span class='line'> touch /var/lock/subsys/sample
+</span><span class='line'> ;;
+</span><span class='line'> stop<span class="o">)</span>
+</span><span class='line'> <span class="c"># Stop something.</span>
+</span><span class='line'> /bin/rm -f /var/lock/subsys/sample
+</span><span class='line'> ;;
+</span><span class='line'> restart|reload<span class="o">)</span>
+</span><span class='line'> <span class="c"># Restart something.</span>
+</span><span class='line'> ;;
+</span><span class='line'> status<span class="o">)</span>
+</span><span class='line'> <span class="c"># Report the running status of something.</span>
+</span><span class='line'> ;;
+</span><span class='line'> *<span class="o">)</span>
+</span><span class='line'> <span class="c"># Invalid command, complain an error.</span>
+</span><span class='line'> ;;
+</span><span class='line'><span class="k">esac</span>
+</span><span class='line'>
+</span><span class='line'><span class="nb">exit </span>0
+</span></code></pre></td></tr></table></div></figure>
+
+
+<h3>解釋</h3>
+
+<p>假設本服務腳本文件的文件名為<strong>sample</strong>。</p>
+
+<p>整個服務腳本是一個普通的Bash腳本。主體是一個case語句,定義了四個命令start、stop、restart或reload、status,顧名思義,四個命令分別用來啟動、停止、重啟該服務,以及獲取服務的運行狀態。</p>
+
+<p>紅帽系發行版使用chkconfig命令設置服務自動在哪個運行級別被自動執行。要使服務腳本支持chkconfig命令,需要加入註釋行:</p>
+
+<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># chkconfig: 345 99 12</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+<p>其中,<strong>345</strong>表示缺省的運行級別,使用如下命令添加服務時:</p>
+
+<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+</pre></td><td class='code'><pre><code class='bash'><span class='line'>chkconfig --add sample
+</span></code></pre></td></tr></table></div></figure>
+
+
+<p>將和使用如下命令一個效果:</p>
+
+<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+</pre></td><td class='code'><pre><code class='bash'><span class='line'>chkconfig --level 345 sample on
+</span></code></pre></td></tr></table></div></figure>
+
+
+<p><strong>99</strong>表示啟動序號,例如若希望服務B在服務A啟動後啟動,且服務A的啟動序號是98,則服務B的啟動序號應設為大於98的一個整數。</p>
+
+<p><strong>12</strong>表示停止序號,作用與啟動序號相同。</p>
+
+<p>此外,若希望在操作系統關閉前先自動執行服務的關閉命令,需要在<strong>/var/lock/subsys/</strong>目錄中存在與服務名同名的空文件,因此,應在<strong>start</strong>命令中創建一個這樣的文件,並在<strong>stop</strong>命令中刪除之。</p>
+]]></content>
+ </entry>
+
+ <entry>
<title type="html"><![CDATA[讀完《三體——死神永生》]]></title>
<link href="http://0x3f.org/blog/after-reading-three-body-long-live-the-king-of-terrors/"/>
<updated>2012-06-14T22:20:00+08:00</updated>
@@ -933,19 +1104,4 @@
]]></content>
</entry>
- <entry>
- <title type="html"><![CDATA[讀完《Pro Git》]]></title>
- <link href="http://0x3f.org/blog/after-reading-pro-git/"/>
- <updated>2011-11-14T00:00:00+08:00</updated>
- <id>http://0x3f.org/blog/after-reading-pro-git</id>
- <content type="html"><![CDATA[<p>花了大約一周的時間,周日晚上讀完<a href="http://progit.org">《Pro Git》</a>的最後幾頁。</p>
-
-<p>高效、安全的分布式和強大的分枝合併是Git的殺手級特性,这本書在語言上淺顯易懂、簡明扼要,在章節安排上循序漸進,在內容講解上有較深入的挖掘,當屬眼下Git方面最好的教材。此外,这本書的中譯水平也很高,必要的地方還有譯注,能看出譯者不是外行。</p>
-
-<p><a href="https://picasaweb.google.com/lh/photo/RuILnzirn3HwuJzk5Wp2Zw?feat=embedwebsite"><img src="https://lh4.googleusercontent.com/-JaRvkCJzpFE/Tr_4ifkfXMI/AAAAAAAAB0w/K_4h24C2fis/s800/A9781430218333-3d_6.png" height="350" width="277" /></a></p>
-
-<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
-]]></content>
- </entry>
-
</feed>
@@ -7,7 +7,7 @@
<meta name="author" content="晴耕雨讀">
- <meta name="description" content="2012 讀完《三體——死神永生》 Jun 14 青梅煮酒 Comments 讀完《三體——黑暗森林》 May 1 Comments UltraBlog.vim v3.5.0: 內建瀏覽器 Apr 30 Comments UltraBlog.vim v3.4.0: 正則表達式、批量替換和調試模式 &hellip;">
+ <meta name="description" content="2012 怎樣為紅帽系Linux發行版寫系統服務腳本 Jun 19 計算機 Comments 讀完《三體——死神永生》 Jun 14 青梅煮酒 Comments 讀完《三體——黑暗森林》 May 1 Comments UltraBlog.vim v3.5.0: 內建瀏覽器 Apr 30 &hellip;">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
@@ -78,6 +78,26 @@
<section class="archives"><h1 class="year">2012</h1>
<article>
+ <h1 class="title"><a href="/blog/how-to-write-service-scripts-for-redhat-distros/">怎樣為紅帽系Linux發行版寫系統服務腳本</a></h1>
+ <div class="meta">
+ <span class="date">Jun 19</span>
+ <span class="tags">
+
+
+ <a class='category' href='/blog/categories/計算機/'>計算機</a>
+
+
+</span>
+
+ <span class="comments"><a href="/blog/how-to-write-service-scripts-for-redhat-distros/#disqus_thread">Comments</a></span>
+
+ </div>
+</article>
+
+
+
+
+<article>
<h1 class="title"><a href="/blog/after-reading-three-body-long-live-the-king-of-terrors/">讀完《三體——死神永生》</a></h1>
<div class="meta">
<span class="date">Jun 14</span>
Oops, something went wrong.

0 comments on commit 2bec6ba

Please sign in to comment.