<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 有效地分發 cron 任務 當你有許多服務器需要執行相同的 cron 作業時,不在同一時間運行它們通常是個好主意。 如果所有作業都要訪問一個公共服務器,就會給該服務器帶來大量負載, 即使這些服務器不會同時訪問公共服務器,所有服務器也會在同一時間處于繁忙狀態, 這可能會削減它們提供其他服務的能力。 Puppet 的 inline_template 函數允許我們使用 Ruby 的邏輯根據主機名為 cron 作業設置不同的運行時間。 #### 操作步驟 1. 在一個節點中添加如下代碼: ``` define cron_random( $command, $hour ) { cron { $name: command =&gt; $command, minute =&gt; inline_template("&lt;%= (hostname+name).hash.abs %60 %&gt;"), hour =&gt; $hour, ensure =&gt; "present", } } cron_random { "hello-world": command =&gt; "/bin/echo 'Hello world'", hour =&gt; 2, } cron_random { "hello-world-2": command =&gt; "/bin/echo 'Hello world'", hour =&gt; 1, } ``` 2. 運行 Puppet: ``` # puppet agent --test info: Retrieving plugin info: Caching catalog for cookbook.bitfieldconsulting.com info: Applying configuration version '1305713506' notice: /Stage[main]//Node[cookbook]/Cron_random[hello-world]/ Cron[hello-world]/ensure: created notice: /Stage[main]//Node[cookbook]/Cron_random[hello-world-2]/ Cron[hello-world-2]/ensure: created notice: Finished catalog run in 1.07 seconds ``` 3. 檢查 crontab 查看是否成功地配置了 cron 作業: ``` # crontab -l # HEADER: This file was autogenerated at Fri Jul 29 10:58:45 +0000 2011 by puppet. # HEADER: While it can still be managed manually, it is definitely not recommended. # HEADER: Note particularly that the comments starting with 'Puppet Name' should # HEADER: not be deleted, as doing so could cause duplicate cron jobs. # Puppet Name: hello-world 25 2 * * * /bin/echo 'Hello world' # Puppet Name: hello-world-2 49 1 * * * /bin/echo 'Hello world' ``` #### 工作原理 我們想要為每個 cron 作業選擇一個 **隨機的** 執行分鐘數; 而不是真正的隨機 (或者說,不是每次運行 Puppet 都會改變 cron 作業的運行時間), 但這也或多或少地保證了每個主機上的每個 cron 作業運行時間的不同。 我們可以使用 Ruby 的 hash 方法實現它,它會對任何對象(本例為一個字符串)計算出一個哈希值。 盡管看上去這個哈希值是隨機的,但它每次運行時都相同,所以當再次運行 Puppet 時其值不會改變。 哈希值生成的是一個大整數,而我們想要的是一個 0 到 59 之間的整數,所以我們使用了 Ruby 的 % (模)運算符將其結果限制在這個范圍內。因為只有 60 種可能的值,盡管 hash 函數被設計為盡可能產生隨機的輸出,還是會有些許的碰撞而且這些碰撞對于 minute 應該是均勻分布的。 因為我們希望每個哈希值在不同的主機上是不同的,所以使用主機名做 hash 處理。 然而,我們還希望同一臺主機上的不同作業的哈希值也不同,所以聯合使用了主機名和作業名 (例如 hello-world)做 hash 處理。 #### 更多用法 在本例中,我們僅對 cron 作業的 minute 進行了隨機化,并將 hour 作為 define 定義的一部分。若你同時希望指定要在周幾運行,可以在 cron_random 中添加一個附加參數來指定, 可以像下面這樣為其指定默認值: ``` define cron_random( $command, $hour, $weekday = "*" ) { ``` 若你想要對 cron 作業的 hour 進行隨機化(例如:要做的作業可以在一天之內的任何時間執行, 并且必須將它們均勻分布在所有的 24 個小時上),可以對 cron_random 做如下修改: ``` hour => inline_template("<%= (hostname+name).hash.abs % 24 %>"), ``` #### 參見本書 * 第 1 章的 [從 cron 運行 Puppet](#ch01sec05) 一節
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看