<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>起動時 &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/%E8%B5%B7%E5%8B%95%E6%99%82/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Sun, 02 Jun 2024 06:56:31 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://sugublog.org/wp-content/uploads/2021/07/icon_20210703-min-150x150.png</url>
	<title>起動時 &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【Linux】起動時にコマンド実行する方法！シェルスクリプト実行も可能</title>
		<link>https://sugublog.org/linux-startup-command-execution/</link>
					<comments>https://sugublog.org/linux-startup-command-execution/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sat, 04 May 2024 14:02:58 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[サーバ]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[コマンド実行]]></category>
		<category><![CDATA[起動時]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6700</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/server_007-5-1024x723.jpg" class="webfeedsFeaturedVisual" /></p>Linuxを運用していて、サーバー起動時にコマンド実行できれば楽ではないでしょうか？ Windowsにはスタートアップというものがありますが、Linuxでも可能です。 という事でLinuxサーバーの起動時にコマンドを実行 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/server_007-5-1024x723.jpg" class="webfeedsFeaturedVisual" /></p>
<p>Linuxを運用していて、<strong><span class="swl-inline-color has-swl-deep-01-color">サーバー起動時にコマンド実行</span></strong>できれば楽ではないでしょうか？</p>



<p>Windowsにはスタートアップというものがありますが、Linuxでも可能です。</p>



<p>という事でLinuxサーバーの起動時にコマンドを実行する方法を、現役エンジニアの筆者が例を元に教えます。</p>


<div class="p-blogParts post_content" data-partsID="5908">
<p>筆者はWindowsサーバーやLinuxサーバーの運用構築を11年間以上おこなってきました。その経験を元に知識を公開していきます！</p>
</div>


<p>windowsのスタートアップのようなことをLinuxでしたい方は、この記事を読んで下さい。</p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">Linuxで起動時にコマンドを実行する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="682" src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg" alt="コマンド実行" class="wp-image-4502" srcset="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-768x511.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>Linuxで<span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">起動時にコマンド実行する方法</span></strong>は「/etc/rc.local」ファイルにコマンドを追記</span>する事です。</p>



<p>起動時に「/etc/rc.local」ファイルに記入されたコマンドを、<span class="swl-bg-color has-swl-deep-01-background-color">root権限で実行</span>してくれます。</p>



<p>実際に「/etc/rc.local」ファイルの中身を見てみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>[root@localhost ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run &#39;chmod +x /etc/rc.d/rc.local&#39; to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local</code></pre></div>



<p>この最終行に起動時実行したいコマンドを追記していきます。</p>



<p>viエディタで追記していきます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>vi /etc/rc.local</code></pre></div>



<p>viエディタで最終行に行くには「G」を押して、「o」で編集モードに変更して記入します。</p>



<p class="is-style-icon_book">viエディタの詳しい使い方については以下の記事参照</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/06/search-min-1-e1623599919108-300x180.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/linux-vi-how-to-use/">【Linux】viの使い方！必須のコマンドから作業効率出来るコマンドまで</a>
						<span class="p-blogCard__excerpt">今回はLinuxサーバーのviエディタの使い方です Linuxsサーバーを使用していて、通常使用するテキストエディタはviエディタです。 しかしviエディタは癖が強く使いにくい&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>以下のコマンドを追記しました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>echo start &gt;&gt; /var/log/startup.log
date &gt;&gt; /var/log/startup.log
echo finish &gt;&gt; /var/log/startup.log</code></pre></div>



<p>「esc」を押してコマンドモードにしてから「:wq」でファイルを保存してviエディタを終了します。</p>



<p class="is-style-icon_book">viエディタの様々な終了方法は以下の記事にまとめています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/06/rocket0627-300x200.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/linux-vi-how-to-quit/">【Linux】viで終了のやり方！保存して終了や保存しないで保存の方法</a>
						<span class="p-blogCard__excerpt">Linuxを使用していて、viエディタの終了のやり方が分からなくなる時があります。 そんな時の為にviの終了のやり方を、現役エンジニアの筆者がまとめます。 保存して終了&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>Linuxサーバーを再起動してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>shutdown -r now</code></pre></div>



<p class="is-style-icon_pen">shutdownコマンドに-rオプションで再起動になります。</p>



<p>Linuxサーバーが立ち上がってきたら以下のコマンドでログを確認します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>cat /var/log/startup.log</code></pre></div>



<p>すると結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>[root@localhost ~]# cat /var/log/startup.log
start
2024年  5月  4日 土曜日 17:39:46 JST
finish</code></pre></div>



<p class="has-border -border02">起動時にコマンドが実行されています。</p>



<h2 class="wp-block-heading">起動時にコマンドが実行されていない場合の原因と対処法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/06/shutterstock_1433996435-min-1024x683.jpg" alt="エラー" class="wp-image-2885"/></figure>
</div>


<p class="is-style-big_icon_batsu">Linuxサーバーの起動時にコマンドが実行されずログに何も記述されない原因は、<span class="swl-marker mark_yellow">「/etc/rc.local」が実行されていない事</span>です。</p>



<p><span class="swl-marker mark_blue">解決方法としては<span class="swl-bg-color has-swl-deep-02-background-color">権限をつける事</span>です。</span></p>



<p>「/etc/rc.local」ファイルは「/etc/rc.d/rc.local」のリンクになっています。</p>



<p>本体の「/etc/rc.d/rc.local」の権限をlsコマンドで見てみます。</p>



<p class="is-style-icon_pen">Linuxのlsコマンドのオプションなど以下の記事でまとめています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-300x200.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/linux-ls-command/">【Linux】lsコマンドの使い方！オプションなどを使用する</a>
						<span class="p-blogCard__excerpt">Linuxサーバーでlsのコマンドの使い方をまとめます。 Linuxサーバーでディレクトリやファイルの表示方法での悩み Linuxサーバーでディレクトリ配下のファイルも表示した&#8230;</span>					</div>
				</div>
			</div>
		</div>


<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>ls -l /etc/rc.d/rc.local</code></pre></div>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="542" height="59" src="https://sugublog.org/wp-content/uploads/2024/05/file権限確認.png" alt="「/etc/rc.d/rc.local」の元の権限状態" class="wp-image-6703" srcset="https://sugublog.org/wp-content/uploads/2024/05/file権限確認.png 542w, https://sugublog.org/wp-content/uploads/2024/05/file権限確認-300x33.png 300w" sizes="(max-width: 542px) 100vw, 542px" /></figure>
</div>


<p class="is-style-big_icon_caution">rootに実行権限が付与されていません。</p>



<p><span class="swl-marker mark_orange">以下のLinuxコマンドで、rootユーザーに実行権限を付与します。</span></p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>chmod 744 /etc/rc.d/rc.local</code></pre></div>



<p>もう一度権限を確認します。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="548" height="123" src="https://sugublog.org/wp-content/uploads/2024/05/file権限追加.png" alt="「/etc/rc.d/rc.local」のrootユーザー実行権限追加時状態" class="wp-image-6704" srcset="https://sugublog.org/wp-content/uploads/2024/05/file権限追加.png 548w, https://sugublog.org/wp-content/uploads/2024/05/file権限追加-300x67.png 300w" sizes="(max-width: 548px) 100vw, 548px" /></figure>
</div>


<p class="is-style-big_icon_check">rootユーザーに実行権限が付与されました。</p>



<p>これで<strong><span class="swl-marker mark_orange">Linuxサーバー起動時にコマンドが実行されます。</span></strong></p>



<h2 class="wp-block-heading">Linuxで起動時にシェルスクリプトを実行する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="560" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg" alt="Linuxでシェルスクリプトを実行" class="wp-image-4561" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-300x164.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-768x420.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><span class="swl-marker mark_green">Linuxサーバー起動時に<strong><span class="swl-inline-color has-swl-deep-03-color">シェルスクリプトを実行</span></strong>したい場合</span>もあると思います。</p>



<p>そんな時は「/etc/rc.local」ファイルに、シェルスクリプトを実行するコマンドを記入するだけです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>sh シェルスクリプトのパス</code></pre></div>



<p>実際にviエディタで下の1行を記述します。</p>



<p>シェルスクリプトを「/opt/shell/startup.sh」に準備します。内容は以下の通りです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="startup.sh"><code>echo startupでshellscript実行 &gt;&gt; /var/log/startup.log</code></pre></div>



<p>それでは「/etc/rc.local」ファイルに下の一行を追加します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>sh /opt/shell/startup.sh</code></pre></div>



<p>これで再起動してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>shutdown -r now</code></pre></div>



<p>Linuxサーバーが立ち上がってきたら以下のコマンドで確認します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>[root@localhost ~]# cat /var/log/startup.log
startupでshellscript実行</code></pre></div>



<p class="has-border -border02">Linux起動時にシェルスクリプトに書いたコマンドが、実行されている事が確認できました。</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">まとめ：Linuxで起動時にコマンドを実行する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/04/server1-3-min-e1623590430388-1024x683.jpeg" alt="サーバー" class="wp-image-2837" srcset="https://sugublog.org/wp-content/uploads/2021/04/server1-3-min-e1623590430388-1024x683.jpeg 1024w, https://sugublog.org/wp-content/uploads/2021/04/server1-3-min-e1623590430388-300x200.jpeg 300w, https://sugublog.org/wp-content/uploads/2021/04/server1-3-min-e1623590430388-768x512.jpeg 768w, https://sugublog.org/wp-content/uploads/2021/04/server1-3-min-e1623590430388.jpeg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">Linuxで起動時にコマンド実行する方法</span></strong>は「/etc/rc.local」ファイルにコマンドを追記する事</span>です。</p>



<p class="is-style-big_icon_point">root権限でコマンドを実行できます。</p>



<p>シェルスクリプトを実行する事も可能。やり方はシェルスクリプトを準備して「/etc/rc.local」にシェルを実行するコマンドを記述する。</p>



<p class="is-style-big_icon_caution"><strong><span class="swl-inline-color has-swl-deep-01-color">起動時にコマンド実行されない場合</span></strong>は、「/etc/rc.local」のリンク先の<span class="swl-marker mark_orange">「/etc/rc.d/rc.local」ファイルにrootユーザー実行権限を与える。</span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/linux-startup-command-execution/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
