Linux配置开机自启动执行脚本的两种方法

WBOY
发布: 2024-06-25 16:49:27
原创
760 人浏览过

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中文网其他相关文章!

来源:linuxprobe.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板