[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,该文件为链接文件
[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>
已开机自启动该脚本
删除掉方法一的配置
[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中文网其他相关文章!