首頁  >  文章  >  系統教程  >  Linux配置開機自啟動執行腳本的兩種方法

Linux配置開機自啟動執行腳本的兩種方法

WBOY
WBOY原創
2024-06-25 16:49:27772瀏覽

Linux配置開機自啟動執行腳本的兩種方法

開機要啟動的腳本qidong.sh

[root@c69-01 scripts]<span class="hljs-comment"># vim /server/scripts/qidong.sh</span>

[root@c69-01 scripts]<span class="hljs-comment"># cat /server/scripts/qidong.sh</span>
<span class="hljs-meta">#!/bin/bash</span>
/bin/<span class="hljs-built_in">echo</span> $(/bin/date +%F_%T) >> /tmp/qidong.log

方法一:修改/etc/rc.local

/etc/rc.local,該檔案為連結檔案

[root@c69<span class="hljs-number">-01</span> ~]# ll /etc/rc.<span class="hljs-keyword">local</span> 
lrwxrwxrwx. <span class="hljs-number">1</span> root root <span class="hljs-number">13</span> Feb  <span class="hljs-number">5</span> <span class="hljs-number">10</span>:<span class="hljs-number">03</span> /etc/rc.<span class="hljs-keyword">local</span> -> rc.d/rc.<span class="hljs-keyword">local</span>

修改/etc/rc.local檔案

[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> scripts]<span class="hljs-comment"># tail -n 1 /etc/rc.local </span>
/bin/bash /server/scripts/qidong.sh ><span class="hljs-regexp">/dev/null</span> <span class="hljs-number">2</span>><span class="hljs-regexp">/dev/null</span>

重啟系統,查看結果

<span class="hljs-selector-attr">[root@c69-01 ~]</span># <span class="hljs-selector-tag">cat</span> /<span class="hljs-selector-tag">tmp</span>/<span class="hljs-selector-tag">qidong</span><span class="hljs-selector-class">.log</span> 
<span class="hljs-selector-tag">2018-02-19_23</span><span class="hljs-selector-pseudo">:30</span><span class="hljs-selector-pseudo">:56</span>

已開機自啟動此腳本

方法二:chkconfig管理

刪除掉方法一的配置

[root@c69-01 ~]<span class="hljs-comment"># vim /etc/init.d/test </span>
<span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># chkconfig: 3 88 88</span>
/bin/bash /server/scripts/qidong.sh >/dev/null 2>/dev/null

[root@c69-01 ~]<span class="hljs-comment"># chmod +x /etc/init.d/test</span>

加入chkconfig,開機自啟動

[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> ~]<span class="hljs-comment"># chkconfig --add test</span>
[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> ~]<span class="hljs-comment"># chkconfig --list test</span>
test           	<span class="hljs-number">0</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">1</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">2</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">3</span><span class="hljs-symbol">:on</span>	<span class="hljs-number">4</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">5</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">6</span><span class="hljs-symbol">:off</span>

重啟系統,查看結果

<span class="hljs-selector-attr">[root@c69-01 ~]</span># <span class="hljs-selector-tag">cat</span> /<span class="hljs-selector-tag">tmp</span>/<span class="hljs-selector-tag">qidong</span><span class="hljs-selector-class">.log</span> 
<span class="hljs-selector-tag">2018-02-19_23</span><span class="hljs-selector-pseudo">:30</span><span class="hljs-selector-pseudo">:56</span>
2018-02-19_23:59:10

操作成功

關閉開機啟動

[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> ~]<span class="hljs-comment"># chkconfig test off</span>
[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> ~]<span class="hljs-comment"># chkconfig --list test</span>
test           	<span class="hljs-number">0</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">1</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">2</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">3</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">4</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">5</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">6</span><span class="hljs-symbol">:off</span>

從chkconfig管理中刪除test

[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> ~]<span class="hljs-comment"># chkconfig --list test</span>
test           	<span class="hljs-number">0</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">1</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">2</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">3</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">4</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">5</span><span class="hljs-symbol">:off</span>	<span class="hljs-number">6</span><span class="hljs-symbol">:off</span>

[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> ~]<span class="hljs-comment"># chkconfig --del test</span>

[root<span class="hljs-variable">@c69</span>-<span class="hljs-number">01</span> ~]<span class="hljs-comment"># chkconfig --list test</span>
service test supports chkconfig, but is <span class="hljs-keyword">not</span> referenced <span class="hljs-keyword">in</span> any runlevel (run <span class="hljs-string">'chkconfig --add test'</span>)

 以上2種開機自啟動腳本的方法,僅供參考,讀者可依自己的狀況,進行設定!

註:我使用的系統為CentOS release 6.9 (Final)

以上是Linux配置開機自啟動執行腳本的兩種方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn