Home  >  Article  >  System Tutorial  >  Two ways to configure Linux to automatically start executing scripts at boot

Two ways to configure Linux to automatically start executing scripts at boot

WBOY
WBOYOriginal
2024-06-25 16:49:27773browse

Two ways to configure Linux to automatically start executing scripts at boot

Script qidong.sh to be started at startup

[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

Method 1: Modify /etc/rc.local

/etc/rc.local, this file is a link file

[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>

Modify the /etc/rc.local file

[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>

Restart the system and check the results

<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>

This script has been started automatically after booting up

Method 2: chkconfig management

Delete the configuration of method 1

[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>

Add to chkconfig and start automatically at boot

[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>

Restart the system and check the results

<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

Operation successful

Turn off and start up

[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>

Remove test from chkconfig management

[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>)

The above two methods of self-starting scripts at boot are for reference only. Readers can configure them according to their own circumstances!

Note: The system I am using is CentOS release 6.9 (Final)

The above is the detailed content of Two ways to configure Linux to automatically start executing scripts at boot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn