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配置开机自启动执行脚本的两种方法的详细内容,更多请关注小闻网其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。